C# is trying to interpret \d
as an escape sequence, and \d
is not a valid escape sequence (but \n
and \t
are, for example). You can either double up the backslashes to escape it ("^(\\d){4}$"
), or you can prefix the constant string with an at-sign: @"^(\d){4}$"
.