DjangoBlockchain
DjangoBlockchain

Reputation: 564

Regular expression for exact price in a string

I am trying to write a regular expression for checking if there is an exact price in a string.

The regex I had created that worked for the exact situation was

.*?0,00

As I am trying to capture when a price is exactly EUR 0,00.

But I am running into issues with prices like EUR 60,00, etc, as it is still matching the 0.00.

How would I create a regular expression to match exactly 0,00?

Upvotes: 0

Views: 63

Answers (2)

The fourth bird
The fourth bird

Reputation: 163277

Your pattern .*?0,00 does not contain boundaries and will match any char except a newline 0+ times non greedy followed by matching 0,00

You could make use of word boundaries \b

\b0,00\b

Or if a negative lookahead and lookbehind is supported, you could assert what is on the left and on the right is not a non whitespace char \S to not get a partial match in ,0,00,

(?<!\S)0,00(?!\S)

Upvotes: 1

Nick Reed
Nick Reed

Reputation: 5059

You can use lookarounds to assert that the character preceding 0,00 is not a digit, and the character following 0,00 is not a digit.

(?<!\d)0,00(?!\d)

Demo

Upvotes: 0

Related Questions