Alok Nath Saha
Alok Nath Saha

Reputation: 357

Multiple Regex Matches Using Negative LookAhead

I'm trying to match the word "query" in the given string "The query resolution with this query management should not get highlighted. Only this query should get highlighted. "

using the following regex:

(query(?!\smanagement)|query(?!\sresolution))

But I'm unable to get the regex to match only the last word "query" in the string.

Regards,

Alok

Upvotes: 1

Views: 936

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626946

You (query(?!\smanagement)|query(?!\sresolution)) regex fails to match only one query and matches all the 3 querys because you have two alternatives: query that should not be followed with a space and management and another alternative matching a query that is not followed with a space and resolution. You need to use one lookahead that will disallow both resolution AND management.

You can use

query(?!\s(?:management|resolution))

See demo

The lookahead (?!\s(?:management|resolution)) will fail the match of query that is followed with 1 whitespace followed with either management or resolution.

To only match whole words, use \b:

\bquery\b(?!\s\b(?:management|resolution)\b)

Python demo showing how you can get the first match in a string with this regex with re.search:

import re
p = re.compile(r'query(?!\s*(?:management|resolution))')
test_str = "The query resolution with this query management should not get highlighted. Only this query should get highlighted.The query resolution with this query management should not get highlighted. Only this query should get highlighted."
m = p.search(test_str)
if m:
    print(m.group())

Upvotes: 2

Related Questions