2

I am trying to write a Reg Expression to match any word from a list of words but am having trouble with words with brackets.

This is the reg expression I have so far:

^\b(?:Civil Services|Assets Management|Engineering Works (EW)|EW Maintenance|Ferry|Road Maintenance|Infrastructure Planning (IP)|Project Management Office (PMO)|Resource Recovery (RR)|Waste)\b$

Words with brackets such as Civil Services are matched but not words with brackets such as Engineering Works (EW).

I have tried single escaping with \ and double escaping (\) but neither option seems to return a match when testing words with brackets in them.

How can I also match words with brackets?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
masterg174
  • 143
  • 9
  • Show a [mcve]. Which language are you using? – user202729 Feb 17 '21 at 01:45
  • At the moment I am just testing on https://regex101.com/ The word list above is just an example but I am intending to use it in a product called Adaxes and I want to perform an action based on a list of departments. If AD Department Matches regexp (...) then create mailbox. The syntax may differ slightly but just hoping to get a bit of an idea of how I can escape brackets. – masterg174 Feb 17 '21 at 01:49

1 Answers1

1

The problem is that \b can't match a word boundary the way you want when it's preceded by a ). A word boundary is a word character adjacent to a non-word character or end-of-string. A word character is a letter, digit, or underscore; notably, ) is not a word character. That means that )\b won't match a parenthesis followed by a space, nor a parenthesis at the end of the string.

The easiest fix is to remove the \bs. You don't actually need them since you've already got ^ and $ anchors:

^(?:Orange|Banana|Apple \(Red\)| Apple \(Green\)|Plum|Mango)$

Alternatively, if you want to search in a larger string you could use a lookahead to look a non-word character or end-of-string. This is essentially what \b does except we only look ahead, not behind.

\b(?:Orange|Banana|Apple \(Red\)| Apple \(Green\)|Plum|Mango)(?=\W|$)
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • I don't know if the OP even wants this, but just use `(?<!\S)` and `(?!\S)` in place of `\b` word boundaries. This way, you can place them only once, outside of the alternation. – Tim Biegeleisen Feb 17 '21 at 01:54