Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for integer or double values

Tags:

c#

regex

I'm trying to make a regex to accept both integer and double values (positive and negative).

This is what I have:

 @"-?d*(?:\d*\.\d*)?$"

Before this is marked as a duplicate question, with all the questions I've checked in SO I end up with the SAME PROBLEM:

5 and -5 -> true

5.00 and -5.00 -> true

BUT... 5. and -5. -> true

It takes < numbers + point only >, in this case ''5.'' and ''-5.'' as a "valid" numbers. Again, this very same problem was present in other similar answers I've already compiled.

like image 649
soulblazer Avatar asked Oct 15 '25 11:10

soulblazer


1 Answers

This should works for you:

@"-?\d+(?:\.\d+)?"

Matchs only the dot only when have digits after it.

like image 66
Arturo Menchaca Avatar answered Oct 17 '25 02:10

Arturo Menchaca



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!