Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace chars if not match

I'm looking for a regular expression that removes illegal characters. But I don't know what the characters will be.

For example:

In a process, I want my string to match ([a-zA-Z0-9/-]*). So I would like to replace all characters that don't match the regexp above.

like image 828
LarZuK Avatar asked Dec 16 '10 11:12

LarZuK


People also ask

Can regex replace characters?

They use a regular expression pattern to define all or part of the text that is to replace matched text in the input string. The replacement pattern can consist of one or more substitutions along with literal characters. Replacement patterns are provided to overloads of the Regex.

When there is no match found for a replace command?

The REPLACE command requires one of the two possible actions take place: If no matching value is found with the existing data row, then a standard INSERT statement is performed. If the duplicate record found, the replace command will delete the existing row and then adds the new record in the table.

How do you match a character except one?

To match any character except a list of excluded characters, put the excluded charaters between [^ and ] . The caret ^ must immediately follow the [ or else it stands for just itself. The character '. ' (period) is a metacharacter (it sometimes has a special meaning).

Does Indexof take regex?

indexOf() that takes a regular expression instead of a string for the first first parameter while still allowing a second parameter ? str. lastIndexOf(/[abc]/ , i); While String.search() takes a regexp as a parameter it does not allow me to specify a second argument!


1 Answers

That would be:

[^a-zA-Z0-9/-]+ 

[^ ] at the start of a character class negates it - it matches characters not in the class.

See also: Character Classes

like image 52
Kobi Avatar answered Sep 22 '22 13:09

Kobi