1

How can I write a Regular Expression to match,

    a string which does not contain the underscore "_".
finnw
  • 46,680
  • 24
  • 139
  • 216
Vikrant Chaudhary
  • 10,779
  • 10
  • 53
  • 67

2 Answers2

8

/^[^_]*$/

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

Rob Di Marco
  • 40,674
  • 8
  • 65
  • 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
  • 165,471
  • 36
  • 174
  • 194