48

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
  • 376,812
  • 128
  • 561
  • 623
planetp
  • 14,248
  • 20
  • 86
  • 160
  • 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

71

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
  • 96,171
  • 6
  • 121
  • 214
  • 11
    +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
  • @ThomasGuyot-Sionnest doing plus is easy; `[/[:graph:]]`. but `/` is already in graph. maybe you meant minus the / character? `[^/[:^graph:]]` may work for that; not sure what regex engine git is using – ysth Feb 15 '22 at 18:07
  • @ysh TBH I can't even make sense of what I wrote above - it wasn't that easy obviously but I don't exactly remember what I was struggling with. I will delete that comment. I remember I found a solution though, will have to look back at what I did... IIRC it used the `cntrl` class. – Thomas Guyot-Sionnest Feb 18 '22 at 09:04
  • I finally checked... my use case was doing a git word diff where I could see just individual path component changes (to compare two build logs with slight path modification), and I'm not sure what I tried but it's very possible I wasn't even trying the right thing at first. It appears the default regex for `--word-diff` is `[^[:space:]]+` (I see no diff using it vs plain `--word-diff`) and to split on paths too I just added `/`, so `git diff '--word-diff-regex=[^[:space:]/]+' [...]`. – Thomas Guyot-Sionnest Mar 02 '22 at 04:53