views:

202

answers:

2

How can I write a Regular Expression to match,

    a string which does not contain the underscore "_".
+5  A: 

/^[^_]*$/

The [^] syntax means "do not include any of these characters".

Rob Di Marco
That was simple. What was I thinking? Actually this was a simplification of a large regular expression problem, whose partial answer I found here - http://stackoverflow.com/questions/1240674/I guess I over simplified it while trying to isolate the problem. And couldn't find this simple answer in the mess. Anyway, thanks for enlightening.
Vikrant Chaudhary
A: 

To match a character that is not an underscore you'd use [^_] (the ^ means "not"). So to match a whole string you'd do something like this:

/[^_]+/
yjerem
Wrong. It would match "a_".
Vikrant Chaudhary
It would match the "a" in "a_", yes.
yjerem