Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 带点和引号的正则表达式模式_C#_.net_Regex - Fatal编程技术网

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

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

如何在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-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对象本身?我有现成的模式,但不知道如何在代码中使用它。我应该在模式声明中写什么?