38
votes

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?

1
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: stackoverflow.com/questions/3201689/… ; e.g. [a-z-[aeiou]] in .NET matches a lowercase consonant.polygenelubricants

1 Answers

60
votes

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