[英]Using C# Regular expressions how do i match the vertical bar as a literal?
I'm writing a program in C# using Microsoft Visual Studio, i need the program to match the vertical bar, but when I try to escape it like this "\\|"
我正在使用Microsoft Visual Studio用C#编写程序,我需要该程序匹配竖线,但是当我尝试像这样
"\\|"
对其进行转义时 it gives me an unrecognized escape sequence error. 它给了我一个无法识别的转义序列错误。 What am I doing wrong?
我究竟做错了什么?
In C# 在C#中
string test = "\|";
Is going to fail because this is a C# string escape sequence, and no such escape exists. 将失败,因为这是C#字符串转义序列,并且不存在此类转义。 Because you are trying to include a backslash in the string, you need to escape the slash so the string actually contains a slash:
因为您试图在字符串中包含反斜杠,所以您需要对斜杠进行转义,以便字符串实际上包含斜杠:
string test = "\\|";
What will actually be stored in this string is \\|
实际将存储在此字符串中的是
\\|
The reason you get an unrecognized escape sequence is that backslash is used as an escape character in C# string literals as well as in regex. 之所以得到无法识别的转义序列,是因为反斜杠在C#字符串文字和正则表达式中都用作转义字符。
You have several choices to fix this: 您有几种选择可以解决此问题:
@"\\|"
@"\\|"
, or "\\\\|"
"\\\\|"
内使用第二个转义"\\\\|"
, or [|]
[|]
The third one is my personal favorite, because it does not require counting backslashes. 第三个是我个人最喜欢的,因为它不需要计算反斜杠。
The string is treating "\\|" 字符串正在处理“ \\ |” as an escaped pipe in C#.
作为C#中的转义管道。 Try "\\|"
尝试“ \\ |” to escape the \\ so that the regex actually sees the \\|
逃避\\,以便正则表达式实际看到\\ | you want.
你要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.