Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to find special characters in a String with some exceptions

Tags:

regex

I just had a similar (but not exact) question answered. Now I need help with the question mentioned below.

I want to write a regex which matches a character if its a non word, non digit and non star (*) character. So, the characters [0-9][a-z][A-Z] * should not match and the others should.

I tried writing [\W[^*]] but it doesn't seem to work.

like image 599
arya Avatar asked Feb 13 '09 20:02

arya


People also ask

How do I find special characters in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).

What does ?= * Mean in regex?

?= is a positive lookahead, a type of zero-width assertion. What it's saying is that the captured match must be followed by whatever is within the parentheses but that part isn't captured. Your example means the match needs to be followed by zero or more characters and then a digit (but again that part isn't captured).

What does \f mean in regex?

Definition and Usage The \f metacharacter matches form feed characters.


1 Answers

Try this instead:

[^\w\*]
like image 87
Zach Scrivena Avatar answered Oct 23 '22 01:10

Zach Scrivena