Match a floating point number.
[Update: Fixed a cut/paste issue with the match for + and -.
Many of the comments on the original post spoke of not having sufficient sample strings. I omitted them deliberately, so that the problem requires a bit more work and will, with any luck, be more educational. My hope is that a little more freedom will give me more issues to write about.
Or perhaps I’m just lazy.
Regardless, the floating point number I was thinking of was something like:
It has an optional +/- at the front, at least one digit, and then an optional decimal part. And there’s no internationalization, so you don’t have to worry about characters between digits or “,” instead of “.” as the decimal point. (What is the term used instead of “decimal point” in such countries? Anybody know?)
And, of course, the usual caveat that if you want to validate a floating point number, something like Double.Parse() is likely to be a bit more robust than a regex you write…
So, to start, we need to match the optional sign character. We do that with:
Next one or more digits:
Optionally followed by a decimal point and one or more digits:
which gives us:
As the final regex. If you wanted to, you could easily extend that to add in “E-038” as an allowable suffix.