RegEx to tell if a string does not contain a specific character

if you are looking for a single character in a string, regex seems like a bit of an overkill. Why not just use .IndexOf or .Contains ?


The pattern [^0] will match any character that is not a zero. In both of your examples, the pattern will match the first character ("1"). To test whether the entire string contains no zeros, the pattern should be "^[^0]*$". This reads as follows: Start at the beginning of the string, match an arbitrary number of characters which are not zeros, followed immediately by the end of the string. Any string containing a zero will fail. Any string containing no zeros will pass.


Your solution is half right. The match you see is for the other characters. What you want to say is something like "hey! I do not want to see this character in the entire string".

In that case you do:

Regex.IsMatch("103","^[^0]*$")