I've written a regex to match any number:
- Positive and Negative
- Decimal
- Real Numbers
The following regex does well but there's one drawback
([+-]{1}){0,1}?[d]*(.{1})?[\d]*
It is positive for inputs such as + or - as well. Any pointers will be greatly appreciated. Thanks.
The regex should work with the following inputs
5, +5, -5, 0.5, +0.5, -0.5, .5, +.5, -.5
and shouldn't match the following inputs
+
-
+.
-.
.
Here is the answer by tchrist, works perfectly.
(?:(?i)(?:[+-]?)(?:(?=[.]?[0-9])(?:[0-9]*)(?:(?:[.])(?:[0-9]{0,}))?)(?:(?:[E])(?:(?:[+-]?)(?:[0-9]+))|))
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…