Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.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/4/regex/17.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#_Regex - Fatal编程技术网

C#正则表达式替换不起作用

C#正则表达式替换不起作用,c#,regex,C#,Regex,我试图使用正则表达式替换字符串,但没有替换任何内容。我不确定我做错了什么 System.Text.RegularExpressions.Regex regEx = null; regEx = new System.Text.RegularExpressions.Regex("DefaultStyle.css?x=839ua9"); pageContent = regEx.Replace(htmlPageContent, "DefaultStyleSnap.css?x=742zh2"); ht

我试图使用正则表达式替换字符串,但没有替换任何内容。我不确定我做错了什么

System.Text.RegularExpressions.Regex regEx = null;

regEx = new System.Text.RegularExpressions.Regex("DefaultStyle.css?x=839ua9");
pageContent = regEx.Replace(htmlPageContent, "DefaultStyleSnap.css?x=742zh2");
htmlPageContent是字符串类型,包含呈现方法的html页面内容(在发送到浏览器之前写入)。htmlPageContent字符串肯定包含“DefaultStyle.css?x=839ua9”。当我在VisualStudio中快速观看时,以及当我查看页面源代码时,我可以看到它


为什么正则表达式不替换字符串?

您必须在
正则表达式中使用
\?
\。
而不是
。请检查下面的代码

代码:

使用系统;
公共课程
{
公共静态void Main()
{
字符串htmlPageContent=“Some HTML
DefaultStyle.css?x=839ua9”; 字符串pageContent=“”; System.Text.RegularExpressions.Regex Regex=null; //regEx=newsystem.Text.RegularExpressions.regEx(“DefaultStyle.css?x=839ua9”); regEx=newsystem.Text.RegularExpressions.regEx(@“DefaultStyle\.css\?x=839ua9”); pageContent=regEx.Replace(htmlPageContent,“DefaultStyleSnap.css?x=742zh2”); Console.WriteLine(页面内容); } }
输出:

Some HTML <br> DefaultStyleSnap.css?x=742zh2
一些HTML
DefaultStyleSnap.css?x=742zh2

您可以在中检查输出。

您必须在
正则表达式中使用
\?
\。
而不是
。请检查下面的代码

代码:

使用系统;
公共课程
{
公共静态void Main()
{
字符串htmlPageContent=“Some HTML
DefaultStyle.css?x=839ua9”; 字符串pageContent=“”; System.Text.RegularExpressions.Regex Regex=null; //regEx=newsystem.Text.RegularExpressions.regEx(“DefaultStyle.css?x=839ua9”); regEx=newsystem.Text.RegularExpressions.regEx(@“DefaultStyle\.css\?x=839ua9”); pageContent=regEx.Replace(htmlPageContent,“DefaultStyleSnap.css?x=742zh2”); Console.WriteLine(页面内容); } }
输出:

Some HTML <br> DefaultStyleSnap.css?x=742zh2
一些HTML
DefaultStyleSnap.css?x=742zh2

您可以检入输出。

您需要使用
\
对特殊字符进行转义

System.Text.RegularExpressions.Regex regEx = null;

regEx = new System.Text.RegularExpressions.Regex("DefaultStyle\.css\?x=839ua9");
pageContent = regEx.Replace(htmlPageContent, "DefaultStyleSnap.css?x=742zh2");
您需要显式转义
,这意味着前面的字符为0或更多

至于
,如果不转义它,将匹配任何字符。更准确的做法是避开它,以确保你不匹配类似的东西

DefaultStyle-css?x=839ua9

您需要使用
\
转义特殊字符

System.Text.RegularExpressions.Regex regEx = null;

regEx = new System.Text.RegularExpressions.Regex("DefaultStyle\.css\?x=839ua9");
pageContent = regEx.Replace(htmlPageContent, "DefaultStyleSnap.css?x=742zh2");
您需要显式转义
,这意味着前面的字符为0或更多

至于
,如果不转义它,将匹配任何字符。更准确的做法是避开它,以确保你不匹配类似的东西

DefaultStyle-css?x=839ua9

是一个特殊字符。不应该使用正则表达式。如果需要向正则表达式模式传递文本,请使用
regex.Escape(“DefaultStyle.css?x=839ua9”)
。这是直接替换。为什么要使用正则表达式呢?我建议只使用内置字符串
Replace
函数。
是一个特殊字符。不应该使用正则表达式。如果需要向正则表达式模式传递文本,请使用
regex.Escape(“DefaultStyle.css?x=839ua9”)
。这是直接替换。为什么要使用正则表达式呢?我建议只使用内置的string
Replace
函数。