i tried to use Regex in c# , but the following code throws an exception:
string pattern = "(\d+)([+*/-])(\d+)";
Regex reg = new Regex (pattern);
After a little dig , i found that in c# if you try to match ‘-‘ with [] expression , it must be put as the first character in that bracket , which confused me.
Could someone jump out and explain that to me ?
Appreciate any of your responses.
The
-character takes on a special meaning within a character class[...]to denote a range, so that shorthand expressions like the following work:The
-is only interpreted literally if it is the first character simply because it can’t denote a range because no other value precedes it.Honestly, it should make no difference whether it is the first or the last character.