Regex 101 Discussion I1 - Match a floating point number

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.


Comments (8)

  1. (+-)? doesn't work... it looks for an optional "+-" string

    +?-? works, as does [+-]?

    The analogous term for "decimal point" in countries that use a comma is "decimal comma"

  2. Thanks - fixed the problem.

  3. Looking at it again, you put (+|-)? which of course works. (Time to get some new glasses...)

  4. Larry Smith says:

    I don't know if the following term is actually used in real life (I strongly doubt it), but mathematicians sometimes call "." the "radix point". But admittedly, this is to address other-than-base-10 issues rather than the actual glyph used (radix comma???).

  5. If you need a generic term to designate . or , as a decimal separator, you can use "decimal symbol". At least, that's what the Customize Regional Options dialog in Windows uses.

  6. Maurits,

    It was broken, but I fixed it after your comment

  7. WebMonster says:

    In Hungary, we say "decimal comma" 🙂

  8. Ruben says:

    In the Dutch speaking regions, it's usually just komma (comma) or decimaalteken (decimal sign/symbol). Don't remember that I've ever heard decimaalkomma (decimal comma).

Skip to main content