Reputation: 2077
I need to simply see if there are any matches for a group of special characters in a filename I have already tried all the common regex expressions including the ones below. All of these examples will find any special character except the brackets.
Regex.Match(filename, "[\\[\\]{}!@#]");
// I even separated this out into 3 like this
Regex.Match(filename, "[");
Regex.Match(filename, "]");
Regex.Match(filename, "[{}!@#]");
filename.IndexOfAny("[]{}!@#".ToCharArray()) != -1
What gives?
Upvotes: 1
Views: 2240
Reputation: 137682
Your solution
filename.IndexOfAny("[]{}!@#".ToCharArray()) != -1
is perfect already. Leave escaping regular expressions to Houdini.
Upvotes: 1
Reputation: 2112
Regex.Match(test, @"[\[\]{}!@#]");
Works for me:
string test = "aoeu[aoeu";
Match m = Regex.Match(test, @"[\[\]{}!@#]");
// m.Success == true
Upvotes: 4