Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match scientific notation of numbers but not other letters

I want to write a regex to match all positive double numbers with maximum 2 digits after decimal point.

My first approach was this:

^\\d+(?:\\.\\d{1,2})?$

it works fine for most cases but not for scientific notations, for example 10000000 when it's written as 1.0E7.

I've found an answer here and I made adapted it to my case resulting:

[\\s=]+([+]?(?:0|[1-9]\\d*)(?:\\.\\d*)?(?:[eE][+\\-]?\\d{1,2}))$

but now it returns false for a lot of "good" values.

Any idea how to make it match only positive numerical values with 0 to 2 digits after the decimal point but also the scientific notation of the numbers?

like image 280
Leo Messi Avatar asked Nov 03 '25 21:11

Leo Messi


1 Answers

You copied the exact regex from the other answer which asked for more requirements i.e. matching equation. Removing those with a bit modification you could try:

^[+-]?\d+(?:\.\d*(?:[eE][+-]?\d+)?)?$

Live demo

like image 138
revo Avatar answered Nov 05 '25 16:11

revo



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!