繁体   English   中英

带点和引号的正则表达式模式

[英]Regex pattern with dot and quotation mark

如何在C#中声明正则表达式模式。 模式以引号开头,之后为网址,如下所示

\"www\.mypage\.pl  //<- this is my pattern

string pattern = ? //todo: what should I put there

使用逐字字符串:

string pattern = @"""www\.mypage\.pl";

您可以使用\\反斜杠转义regex语义并还原为实际文字。 如果你想逃跑

"www.mypage.pl

您可以使用

\"[0-9a-zA-Z]*\.[0-9a-zA-Z]*\.[0-9a-zA-Z]*
^ this is not to escape the regex but the eventual string
  if you use single quotes you don't need to escape the quotes!

请注意, [0-9a-zA-Z]*将需要更多的字符,例如%-_以根据有关我现在无法生成的URL语法的RFC捕获所有情况(可在网上轻松找到)。

如果你想逃跑

\"www\.mypage\.pl

您还必须逃脱逃生:

\\\"[0-9a-zA-Z]*\\\.[0-9a-zA-Z]*\\\.[0-9a-zA-Z]*

其他答案已经处理(非常好)如何将短语本身放入字符串中。

要使用它,您需要引用System.Text.RegularExpressions命名空间,有关该文件的信息,请参见MSDN 作为一个(非常)快速的示例:

System.Text.RegularExpressions.Regex.Replace(content,
                                        regexPattern, newMatchContent);

会将content newMatchContent则表达式regexPattern匹配项替换为newMatchContent ,并返回结果。

暂无
暂无

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

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