otto.kranz
otto.kranz

Reputation: 91

RegEx negative lookahead on pattern

I want to find all expressions that don't end with ":" I tried to do it like that:

[a-z]{2,}(?!:)

On this text:

foobar foobaz:
foobaz
foobaz:

The problem is, that it just takes away the last character befor the ":" and not the whole match. Here is the example: https://regex101.com/r/jtLRvz/1

How can I get the negative lookahead work for the whole regular expression?

Upvotes: 1

Views: 175

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627537

When [a-z]{2,}(?!:) matches baz:, [a-z]{2,} grabs 2 or more lowercase ASCII letters at once (baz) and the negative lookahead (?!:) checks the char immediately to the right. It is :, so the engine asks itself if there is a way to match the string in a different way. Since {2,} can match two chars, not currently matched three, it backtracks, and finds a valid match.

Add a-z to the lookahead pattern to make sure the char right after 2 or more lowercase ASCII letters is not a letter and not a colon:

[a-z]{2,}(?![a-z:])
             ^^^

See the regex demo

If your regex engine supports possessive modifiers, or atomic groups, you may use them to prevent backtracking into the [a-z]{2,} subpattern:

[a-z]{2,}+(?!:)
(?>[a-z]{2,})(?!:)

See another regex demo.

Upvotes: 3

Related Questions