C# 带点和引号的正则表达式模式
如何在c#中声明正则表达式模式。模式以引号开始,后面是url地址,如下所示C# 带点和引号的正则表达式模式,c#,.net,regex,C#,.net,Regex,如何在c#中声明正则表达式模式。模式以引号开始,后面是url地址,如下所示 \"www\.mypage\.pl //<- this is my pattern string pattern = ? //todo: what should I put there \“www\.mypage\.pl/您可以使用\反斜杠来转义正则表达式语义并还原为实际文本。如果要转义 "www.mypage.pl \"www\.mypage\.pl 你可以用 \"[0-9a-zA-Z]*\.[0-9a
\"www\.mypage\.pl //<- this is my pattern
string pattern = ? //todo: what should I put there
\“www\.mypage\.pl/您可以使用\
反斜杠来转义正则表达式语义并还原为实际文本。如果要转义
"www.mypage.pl
\"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
\"www\.mypage\.pl
你也必须逃离逃跑:
\\\"[0-9a-zA-Z]*\\\.[0-9a-zA-Z]*\\\.[0-9a-zA-Z]*
使用逐字字符串:
string pattern = @"""www\.mypage\.pl";
其他答案已经(非常好地)处理了如何将短语本身放入字符串中
要使用它,您需要参考System.Text.RegularExpressions
名称空间,其中的文档是。作为一个(非常)快速的示例:
System.Text.RegularExpressions.Regex.Replace(content,
regexPattern, newMatchContent);
将用newMatchContent
替换content
中正则表达式regexPattern
的匹配项,并返回结果。您是在问如何将\'www\.mypage\.pl
放入字符串,还是如何使用RegEx对象本身?我有现成的模式,但不知道如何在代码中使用它。我应该在模式声明中写什么?