Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex101: Check if a floating point number (e.g. 3.14159) is in a valid format.(Question 11)

This is question 11 from the regex101 quiz

I have been trying to figure out a solution to solve the test case below from failing. Can someone help with this?

Test 60/89: Just a dot is not a valid floating number.

I am using the regex below.

/^[+-]?\d*([.,]\d*)?([Ee][+-]?\d+)?$/g

like image 487
gaurav sabharwal Avatar asked Oct 18 '25 07:10

gaurav sabharwal


1 Answers

You can use

/^[-+]?(\d+[,.]|\d*[.,]?\d+)(e[-+]?\d+)?$/i

Details:

  • ^ - start of string
  • [-+]? - an optional - or +
  • (\d+[,.]|\d*[.,]?\d+) - either one or more digits, then , or ., or zero or more digits, an optional .or,` and then one or more digits
  • (e[-+]?\d+)? - an optional occurrence of e, then an optional - or + and then one or more digits
  • $ - end of string.
like image 140
Wiktor Stribiżew Avatar answered Oct 19 '25 20:10

Wiktor Stribiżew



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!