Kevin
Kevin

Reputation: 825

Negative Lookahead not working as expected

I want to match all the numbers not followed by a Doller:

100Dollar
1000Dollar
100Yuan
1000Yuan

the regex:

\d+(?!Dollar)

But the result is weird:

img

Live example:

https://regexr.com/3ko0t

Upvotes: 0

Views: 68

Answers (2)

mrzasa
mrzasa

Reputation: 23307

Add a digit class to the negative lookahead to assure that you match till the then of a number:

\d+(?!Dollar|\d)

Demo

Upvotes: 1

Srdjan M.
Srdjan M.

Reputation: 3405

Regex: (?!\d+Dollar)\d+

Details:

  • (?!) Negative Lookahead
  • \d Matches a digit (equal to [0-9])
  • + Matches between one and unlimited times

Upvotes: 2

Related Questions