簡體   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