**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:

-333.33

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:

\d+

Optionally followed by a decimal point and one or more digits:

(\.\d+)?

which gives us:

^

(\+|-)?

\d+

(\.\d+)?

$

As the final regex. If you wanted to, you could easily extend that to add in "E-038" as an allowable suffix.

(+-)? 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"

Thanks – fixed the problem.

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

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???).

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.

Maurits,

It was broken, but I fixed it after your comment

In Hungary, we say "decimal comma" đź™‚

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).