Regex matching numbers and decimals

regex

Solution

This is a fairly common task. The simplest way I know of to deal with it is this:

^[+-]?(\d*\.)?\d+$

There are also other complications, such as whether you want to allow leading zeroes or commas or things like that. This can be as complicated as you want it to be. For example, if you want to allow the 1,234,567.89 format, you can go with this:

^[+-]?(\d*|\d{1,3}(,\d{3})*)(\.\d+)?\b$

That `\b` there is a word break, but I'm using it as a sneaky way to require at least one numeral at the end of the string. This way, an empty string or a single `+` won't match.

However, be advised that regexes are not the ideal way to parse numeric strings. All modern programming languages I know of have fast, simple, built-in methods for doing that.

Problem

I need a regex expression that will match the following: ``` .5 0.5 1.5 1234 ``` but NOT ``` 0.5.5 absnd (any letter character or space) ``` I have this that satisfies all but `0.5.5` ``` ^[.?\d]+$ ```

Original source

Related problems