Improve Numeric regex to not match empty dot

Viewed 23

I have the following regex to match a number:

^[+-]?[0-9]*\.?[0-9]*(E[+-]?[0-9]*)?$

Shown here: https://regex101.com/r/aAosEj/1. However, this also matches the empty dot, .. Is there a simple way to improve this regex for that, or do I need to handle the case in two ways, one which does num?.num and another which does num.?

1 Answers

Nothing too simple here, but not too bad...

You need two cases, digits+ (. digits*)? OR . digits+ You also need to fix the exponent, which must be present if the E is used.

^[+-]?([0-9]+(\.[0-9]*)?|\.[0-9]+)(E[+-]?[0-9]+)?$
Related