开发者

Regular Expression issue

开发者 https://www.devze.com 2022-12-14 02:48 出处:网络
I have an issue regarding the use of the following regular expression: 开发者_StackOverflow社区private Regex _regexDecimals = new Regex(@\"[^.,0-9]\");

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

开发者_StackOverflow社区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?


Replace the \. with [.,]


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


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]

0

精彩评论

暂无评论...
验证码 换一张
取 消