views:

75

answers:

3

I have an issue regarding the use of the following regular expression:

private Regex _regexDecimals = new Regex(@"[^.,0-9]");

when I use above the result is dat I can use

1 0,5 ,5 1.0

but when I enter .5 it results in an error trying it to convert it to a double.

Now I've made the following regular expression:

private Regex _regexDecimals = new Regex(@"[0-9]+(?:\.[0-9]*)?");

This matches for using the dots, but not for using the comma's. How can I match also using the comma's?

+4  A: 

Replace the \. with [.,]

Anon.
A: 

Have you tried something like this:

public static Regex regex = new Regex(
      "\\d*[.,]?\\d+",
    RegexOptions.IgnoreCase
    | RegexOptions.CultureInvariant
    | RegexOptions.IgnorePatternWhitespace
    | RegexOptions.Compiled
    );

?

The '\d' is the equivalent of [0-9]

Chris
+1  A: 

If your end goal is to cast it to a double you should use Double.TryParse instead.

Yuriy Faktorovich