Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex everything but number with two decimals

I have a nice challenge for those who like regex; like me. Unfortunately, I cannot figure this one out.

This regex is a reverse match. I need to match everything but a proper amount; like:

These types need to NOT MATCH:

0,00
0.00
12314345.7
24234.54
34435,00
34545,43

These types need to MATCH:

.00
,87
1e3,67

So everything but a decimal amount with a comma or point needs to match in JavaScript (yes, reverse).

I have made this:

([^0-9]+([^\.,]{0,1})+[^0-9]{0,2})

But, obviously, it doesn't work properly seeing it passes through multiple comma's or dots and doesn't limit the decimals to a maximum of two.

like image 883
Matt Avatar asked Nov 10 '22 07:11

Matt


1 Answers

Solution using a negative lookahead:

^(?!\d+([,.]\d{0,2})?$).*$

http://regex101.com/r/jY3tC3

Note: This regex needs anchoring. I've anchored between ^ and $.

like image 172
Lodewijk Bogaards Avatar answered Nov 14 '22 21:11

Lodewijk Bogaards