0

I would like to have a regex which identifies all words of the form: xxxxBlah

but to ignore a specific case of: SpecialBlah

Is there any simple way to express this as a regular expression?

Aprillion
  • 16,686
  • 4
  • 48
  • 86
Brother Logic
  • 286
  • 2
  • 9
  • yes if the language of your choice support [negative lookaheads](http://www.regular-expressions.info/lookaround.html) – Aprillion Jul 11 '14 at 21:37

2 Answers2

1

Version without negative lookbehind:

(?!SpecialBlah\b)\b\w+Blah\b

Regular expression visualization

Debuggex Demo

Regex101 Demo

elixenide
  • 42,388
  • 14
  • 70
  • 93
  • 1
    No; it makes no difference where the first `\b` is. – elixenide Jul 11 '14 at 21:44
  • I think there is no need to add `\b` if `\w` is already used that looks for word boundary automatically. – Braj Jul 11 '14 at 21:45
  • @Braj Yes, you do need the `\b`. Otherwise, it matches `pecialBlah` in `SpecialBlah`. It also would see `SpecialBlahs` as a match, because it isn't forced to match the trailing word break. – elixenide Jul 11 '14 at 21:48
0

Depending on the larguage/IDE/tool you're using, this should work:

\w+(?<!Special)Blah

Regular expression visualization

Debuggex Demo

It uses a negative look-behind. So it finds any word-characters before "Blah", as long as it's not "Special".

Others have provided alternatives when a negative lookbehind is not possible, such as in JavaScript.


Please consider bookmarking the Stack Overflow Regular Expressions FAQ for future reference.

Community
  • 1
  • 1
aliteralmind
  • 18,274
  • 16
  • 66
  • 102