37

Say I want to match a "word" character (\w), but exclude "_", or match a whitespace character (\s), but exclude "\t". How can I do this?

polygenelubricants
  • 348,637
  • 121
  • 546
  • 611
planetp
  • 10,603
  • 14
  • 62
  • 124
  • 6
    For the benefit of others who may be using Java, .NET, XML schema, or JGSoft/RegexBuddy, there is actually a character subtraction mechanism for those flavors: http://stackoverflow.com/questions/3201689/character-class-subtraction-converting-from-java-syntax-to-regexbuddy ; e.g. `[a-z-[aeiou]]` in .NET matches a lowercase consonant. – polygenelubricants Aug 23 '10 at 18:05

1 Answers1

57

Use a negated class including \W or \S.

/[^\W_]/  # anything that's not a non-word character and not _
/[^\S\t]/ # anything that's not a non-space character and not \t
ysth
  • 88,068
  • 5
  • 112
  • 203
  • 8
    +1 I call this technique a [double-negative](http://stackoverflow.com/questions/3469080/match-whitespace-but-not-newlines-perl/3469155#3469155). – Greg Bacon Aug 23 '10 at 15:54