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]
精彩评论