C# Regex Issue “unrecognized escape sequence”

前端 未结 3 1580
你的背包
你的背包 2020-12-03 13:38

I have an issue with the regular expressions I\'m using but don\'t know how to continue with them. I get the error \"unrecognized escape sequence\".

I am trying to l

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-03 14:11

    Use @ to make the strings no longer use the escape character \:

    string regexPattern1 = @"^(\d{3}\.){2}\d{4}$";
    string regexPattern2 = @"^((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}$";
    

    As a side note, I think you want the two ifs at the end to be a single if with an or (||) between the two conditions.

提交回复
热议问题