简体   繁体   English

如何使用C#在正则表达式中匹配“

[英]How to match " in Regular expression using C#

I want to match " inside a string but I am not able to add " to the list 我想在字符串中匹配"但我无法在列表中添加"

Current my regular expression is 目前我的正则表达式是

Regex.Replace(str, @"[\\/:*?<>|]","", RegexOptions.Compiled);

I also want to add " 我也想添加"

\\" is not accepted. \\"不被接受。

You need to escape " to "" . 您需要转义" to ""

Use "" with verbatim strings.. 对逐字字符串使用""

ie @"[\\\\/:*?<>|""]" @"[\\\\/:*?<>|""]"

OR 要么

Just use \\" without verbatim strings. 只需使用\\" 而不使用逐字字符串。

ie "[\\\\/:*?<>|\\"]" "[\\\\/:*?<>|\\"]"


A character that is preceded with forward slash \\ is treated as a special character .. 以正斜杠\\开头的special character被视为special character ..

For example.. \\t , \\n , \\r are special characters.. 例如.. \\t\\n\\r是特殊字符。

But \\e is not a special character since e has no special meaning..So,compiler would show you compile time error Unrecognized escape sequence 但是\\e不是特殊字符,因为e没有特殊含义。.因此,编译器将向您显示编译时错误Unrecognized escape sequence

In order to treat characters preceded by \\ literally(ie to make it non special character) we use verbatim string ie @"" 为了按字面意义处理\\前面的字符(即使其成为非特殊字符),我们使用逐字字符串,即@""

You must use \\ to escape it. 您必须使用\\对其进行转义。

For example: \\" 例如: \\"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM