在 C# 中允许反斜杠的正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14641340/
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
Regular expression to allow backslash in C#
提问by Suhaib
Can anyone provide me with regex for validating string which only should not allow any special characters except backslash. I tried
任何人都可以向我提供用于验证字符串的正则表达式,该字符串只应该不允许除反斜杠以外的任何特殊字符。我试过
var regexItem = new Regex("^[a-zA-Z0-9\\ ]*$");
But it doesn't seem to work
但它似乎不起作用
回答by Daniel Hilgarth
You either need to double escape it (once for C# and once for the Regex engine):
您要么需要对它进行双重转义(一次用于 C#,一次用于 Regex 引擎):
var regexItem = new Regex("^[a-zA-Z0-9\\ ]*$");
Or you can use the verbatim string featureof C# (note the @):
或者您可以使用C#的逐字字符串功能(注意 @):
var regexItem = new Regex(@"^[a-zA-Z0-9\ ]*$");
In a verbatim string, the backslash is not interpreted as starting an escape sequence, so you just need to escape it once for the Regex engine.
在逐字字符串中,反斜杠不会被解释为开始转义序列,因此您只需要为 Regex 引擎转义一次即可。
I assume that your current code doesn't compile. It should say something along the lines of "Unrecognized escape sequence".
The reason for this is that you have three backslashes followed by a space. The first two backslashes are interpreted as an escape sequence representing a backslash, but the third backslash is interpreted as starting an escape sequence with a space as the second character. Such an escape sequence doesn't exist, leading to the error.
我假设您当前的代码无法编译。它应该说一些类似于“无法识别的转义序列”的内容。
这样做的原因是您有三个反斜杠后跟一个空格。前两个反斜杠被解释为表示反斜杠的转义序列,但第三个反斜杠被解释为以空格作为第二个字符的转义序列的开始。这样的转义序列不存在,导致错误。
回答by Naveed S
You would have to escape the \
for regex as well as C# string by adding 4\s
for matching a single \
.
您必须\
通过添加4\s
来匹配单个\
.
回答by Jon Skeet
Backslashes need to be escaped in regular expressions - and they alsoneed to be escaped in C#, unless you use verbatim string literals. So either of these should work:
反斜杠需要在正则表达式中转义 - 并且它们也需要在 C# 中转义,除非您使用逐字字符串文字。因此,其中任何一个都应该有效:
var regexItem = new Regex(@"^[a-zA-Z0-9\ ]*$");
var regexItem = new Regex("^[a-zA-Z0-9\\ ]*$");
Both of these ensure that the following string content is passed to the Regex
constructor:
这两者都确保将以下字符串内容传递给Regex
构造函数:
^[a-zA-Z0-9\ ]*$
The Regex
code will then see the double backslash and treat it as "I really want to match the backslash character."
然后Regex
代码将看到双反斜杠并将其视为“我真的想匹配反斜杠字符”。
Basically, you always need to distinguish between "the string contents you want to pass to the regex engine" and "the string literal representation in the source code". (This is true not just for regular expressions, of course. The debugger doesn't help by escaping in Watch windows etc.)
基本上,您始终需要区分“要传递给正则表达式引擎的字符串内容”和“源代码中的字符串文字表示”。(当然,这不仅适用于正则表达式。调试器不会通过在监视窗口等中转义来提供帮助。)
EDIT: Now that the question has been edited to show that you originally had threebackslashes, that's just not valid C#. I suspectyou were aiming for "a string with three backslashes in" which would be either of these:
编辑:既然问题已被编辑以显示您最初有三个反斜杠,那只是无效的 C#。我怀疑你的目标是“一个带有三个反斜杠的字符串”,这将是以下之一:
var regexItem = new Regex(@"^[a-zA-Z0-9\\ ]*$");
var regexItem = new Regex("^[a-zA-Z0-9\\\ ]*$");
... but you don't need to escape the space as far as the regular expression is concerned.
...但就正则表达式而言,您不需要逃避空间。