I have a string 1.5(+1.2/-0.5). I want to use Regex to extract numerical value: {1.5, 1.2, 0.5}.
My plan is to split the string with (, +, / and -. When I do split with ( and /, it splits OK, but if I also add + and -, then program crashes.
string[] foo = Regex.Split("1.5(+1.5/-0.5)", @"(?=[(/)])");
// OK
string[] foo = Regex.Split("1.5(+1.5/-0.5)", @"(?=[(/+-)])");
// Exception catched
And the caught exception is:
System.ArgumentException: parsing “(?=[(/+-)])” – [x-y] range in
reverse order
The dash is a special character when inside square brackets in a regexp. It means a range:
[a-z]means any character fromatoz. When you wrote[(/+-)], it would actually mean(, or any character from+to). The error comes from the fact that in ASCII ordering)comes before+, so a character range[+-)]is invalid.To fix this, dash must always come first or last when in brackets, or it needs to be backslashed.
And I agree, I’d probably use a global regexp to pick out
[0-9.]+, and not a split to cut on everything else.