I have a string and I want to make sure it only contains characters in BSEO|0123456789. I tried to do that with this regex
Regex r = new Regex("^[BSEO|0123456789]");
if (r.IsMatch(str)) throw new Exception("Invalid character.");
but it doesn’t behave correctly, (e.g. string SE throws exception). What am I doing wrong, how can I fix it, and is there a better solution to this than using regex?
You can use linq:
EDIT
After pondering this for a while, I think the call to
Distinct()may be a failed attempt at micro-optimization. If performance is important, try with and without. It may well perform better without.EDIT 2
I am now convinced that
Distinct()makes the query run more slowly. Use this instead: