C# 错误的编译常量值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1558058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bad Compile constant value
提问by PositiveGuy
I get "Bad Compile constant value" on this statement.
我在这条语句上得到“错误的编译常量值”。
Regex objCheckNumber = new Regex("^(\d){4}$");
I simply want to set this up to check another string to see if the value entered is 4 digits.
我只是想设置它来检查另一个字符串以查看输入的值是否为 4 位数字。
采纳答案by Mark Rushakoff
C# is trying to interpret \d
as an escape sequence, and \d
is nota 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#试图解释\d
为转义序列,以及\d
是不是一个有效的转义序列(但\n
并\t
是,例如)。您可以将反斜杠加倍以将其转义 ( "^(\\d){4}$"
),或者您可以在常量字符串前添加 at 符号:@"^(\d){4}$"
。
回答by Jason Kresowaty
C# uses \ as an escape character. You need to double up the \
to \\
.
C# 使用 \ 作为转义字符。你需要加倍的\
来\\
。
Alternatively, place a @ character before the double-quote:
或者,在双引号之前放置一个 @ 字符:
new Regex(@"^(\d){4}$")