stanek
stanek

Reputation: 592

Match all words that appear within parenthesis except for certain phrases

My matching Regex for capturing all words within parenthesis (including the parenthesis) is

\([\w\s,.]+\)

I need to ignore the capture when (City of Toronto) appears

Lorem ipsum dolor sit amet, (Capture This) consectetur adipiscing elit. Integer nulla nisi, viverra ut eros vitae, bibendum elementum quam (City of Toronto). Morbi quam libero, sollicitudin et sapien et, lacinia (Capture this as well) euismod tellus.

I would want the matches to be #1- (Capture This) and #2- (Capture this as well)

Essentially I'd like to create a white list of phrases to avoid but if they don't match, then capture the text.

Upvotes: 0

Views: 38

Answers (1)

The fourth bird
The fourth bird

Reputation: 163277

Maybe you could use a negative lookahead to assert that what follows is not (City of Toronto) (?!\(City of Toronto\))\([\w\s,.]+\)

You can use alternation | to add multiple options.

The slightly faster version from @ctwheels with the alternation:

\((?!City of Toronto|Hong Kong|United Kingdom\))[\w\s,.]+\)

Upvotes: 3

Related Questions