1

How can I write a Regular Expression to match,

    a string which does not contain the underscore "_".
finnw
  • 47,861
  • 24
  • 143
  • 221
Vikrant Chaudhary
  • 11,089
  • 10
  • 53
  • 68

2 Answers2

9

/^[^_]*$/

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

Rob Di Marco
  • 43,054
  • 9
  • 66
  • 56
  • 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 Aug 13 '09 at 03:57
0

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:

/[^_]+/
Paige Ruten
  • 172,675
  • 36
  • 177
  • 197