ARMATAV
ARMATAV

Reputation: 634

Negative lookahead after a match

I'm using this regex to check the following block of text.

((?<=(over the)).*)?(ask.*question[s]?| answer(ing)?.*question[s]?)(?!(business))

But I'd like to exclude this block because it has business in it, so I modified it to include a negative lookahead (?!(business)).

It should exclude this block;

I just need to ask you a few questions to recommend the best option for you. What kind of business do you have?

It should include this block;

I just need to ask you a few questions to recommend the best option for you.

But negative lookahead seems to not be working?

Upvotes: 1

Views: 90

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626825

Instead of (?!(business)) you may use (?!.*business) since the substring business is not right after the match, but after zero or more other chars.

Use

((?<=over the).*)?(ask.*questions?| answer(?:ing)?.*questions?)(?!.*business)
                                                               ^^^^^^^^^^^^^^

See the regex demo.

Upvotes: 1

Related Questions