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:
"[a-z]" // matches all lowercase alphabetic characters without having to specify them all.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With