I was trying to strip all non-numeric characters from a phone number.
So, suppose we have numbers in this format: “(123) 456-7890”.
Regex.Replace(phone, @"[ -()]", string.Empty)
leaves the dash in. Same with () escaped (“\(\)”).
This
Regex.Replace(phone, @"[() -]", string.Empty)
works, though.
I thought that order of characters in [] doesn’t play a role ? In fact, I get same results if I switch space and dash around.. But brackets have to go first to work ?
-can be used to declare a range, e.g.,[a-z]. You need to escape it:[ \-()].The exception is if you put the dash at an end of the character group, where its only meaning can be a literal
-.