Escape brackets in a regex with alternation

926 views Asked by At

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?

1

There are 1 answers

1
John Kugelman On BEST ANSWER

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|$)