I need to build regex dynamically, so I pass to my method a string of valid characters. Then I use that string to build regex in my method
string valid = "^m><"; // Note 1st char is ^ (special char)
string input = ...; //some string I want to check
Check(valid);
public void Check(string valid)
{
Regex reg = new Regex("[^" + valid + "]");
if (reg.Match(input).ToString().Length > 0)
{
throw new Exception(...);
}
}
I want above Match to throw exception if it finds any other character than characters provided by valid
string above. But in my case, even if I dont have any other character tahn these 3, Check method still throws new exception.
What is wrong with this regex?