开发者

Bad Compile constant value

开发者 https://www.devze.com 2022-12-08 22:05 出处:网络
I get \"Bad Compile constant value\" on this statement. Regex objChe开发者_StackOverflowckNumber = new Regex(\"^(\\d){4}$\");

I get "Bad Compile constant value" on this statement.

Regex objChe开发者_StackOverflowckNumber = new Regex("^(\d){4}$");

I simply want to set this up to check another string to see if the value entered is 4 digits.


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}$".


C# uses \ as an escape character. You need to double up the \ to \\.

Alternatively, place a @ character before the double-quote:

new Regex(@"^(\d){4}$")
0

精彩评论

暂无评论...
验证码 换一张
取 消