0

I have the following regex:

Regex rgx = new Regex("[^a-z0-9/ -_]");
input = rgx.Replace(input, string.Empty);

Which I thought would remove anything that is not a to z, 0 - 9, a space, dash or underscore.

However, when I pass the following in "Outfalls & Accessories", it doesn't remove the ampersand

Is there something I am missing here?

Pete
  • 57,112
  • 28
  • 117
  • 166
  • 1
    The `-` inside a character class creates a range. `[ -_]` matches [*a single character in the range between (index 32) and _ (index 95)*](https://regex101.com/r/2cYQJ5/1). So, a lot more chars will be affected. Either escape it or move to the start/end of the character class, or right before or after a valid range. – Wiktor Stribiżew Mar 09 '18 at 12:31
  • Ah ok, thanks - that's solved it – Pete Mar 09 '18 at 12:33

0 Answers0