4

The regular expression should match below criteria. The number of elements before and after the dot can be any. Only 1 dot is allowed and negative sign is allowed at first position only. I do not require comma.

Example:

1
-1
-1.
1.
1.2
-.2
-0.2
000.300

All above expressions should result true.

So if i break up..

  1. An optional negative sign at first place.
  2. Zero or more digit before dot.
  3. Dot is optional. Can occur max one time. It can be pure integer number also.
  4. O or more digits after dot.

Any help will be appreciated.

iain
  • 11,098
  • 9
  • 43
  • 93
Jason
  • 113
  • 1
  • 4
  • 15
  • 1
    http://regexlib.com/Search.aspx?k=decimal i love that site for finding pretested good regex :) here is one: ^[-+]?\d+(\.\d+)?$ – Tony Oct 22 '13 at 21:15
  • 5
    Why using regex? You can try `decimal.TryParse()` instead. – MarcinJuraszek Oct 22 '13 at 21:16
  • Possible duplicate of http://stackoverflow.com/questions/11896274/regular-expression-for-validating-decimal-numbers and a few others – Konrad Morawski Oct 22 '13 at 21:19
  • @MarcinJuraszek that's right, everyone is using dotNet… ;) – iain Oct 22 '13 at 21:58
  • 2
    @iain The question was tagged with `C#` tag and had `C#` in title before. That's why I posted my comment. Btw. I don't know why @konrad deleted that tag... – MarcinJuraszek Oct 22 '13 at 21:59
  • @MarcinJuraszek fair enough, my apologies :) – iain Oct 23 '13 at 00:22
  • Would love to have known if you had gone to any lengths to learn regex yourself. A quick google search would have helped you in a fraction of the time – Gusdor Oct 23 '13 at 15:01
  • possible duplicate of [Decimal number regular expression](http://stackoverflow.com/questions/12117024/decimal-number-regular-expression) – Gusdor Oct 23 '13 at 15:01
  • possible duplicate of [Matching numbers with regular expressions — only digits and commas](http://stackoverflow.com/questions/4246077/matching-numbers-with-regular-expressions-only-digits-and-commas) – HamZa Dec 25 '13 at 19:39

3 Answers3

5

What you probably want is this:

^-?\d*\.?\d*

Which will give you a possible negative sign (-?),
followed by any number of digits (\d*),
followed by a possible decimal point (\.),
followed by any number of trailing digits after the decimal point (\d*).

Since you just want to validate whether it's a valid float or not, @MarcinJuraszek has a good point, you may not want to be using regex here.

eric
  • 205
  • 2
  • 12
  • With this solution what `Regex` method would you use? I am currently using this with `IsMatch` like so: `Regex.IsMatch(string, @"^-?\d*\.?\d*")` and not getting any results. – Eric after dark Jun 10 '14 at 20:26
  • It accepts an input of ( 4. ) which is invalid. If there is a dot there must be a digit after it. How do you do that? – Pouya BCD Feb 18 '15 at 16:33
2

1) An optional negative sign at first place:

^ : Start of string

- : The minus

? : Makes the preceeding character optional

2) Zero or more digits

/d : digit

* : match as many (including zero) of the previous thing

3) Optional dot

. : the dot

? : makes the dot optional

4) 0 or more details after dot

/d : digit

* : match as many (including zero) of the previous thing

So all together: ^-?/d*.?/d*

hankd
  • 648
  • 3
  • 11
0

Here is my solution

Pros

  • User can specify precision
  • Accept comma or dot
  • Accept negative values
  • Accept integers

Cons

  • doesn't work for .0 or 0. :(

    [-]?\d{1,18}(?:[,.]\d{1,2})?$

user1075940
  • 976
  • 2
  • 20
  • 41