2012-05-28 68 views
2

在我的代码中,我找到了所有匹配元素并用特殊值替换它。如何正确替换字符串

Regex imgRule = new Regex("img id=\\\".+?\\\""); 
        MatchCollection matches = imgRule.Matches(content.Value); 
        string result = null; 
        foreach (Match match in matches) 
         result = match.Value; 

        if (result != null) 
        { 
         var firstOrDefault = node.ListImages.FirstOrDefault(); 
         if (firstOrDefault != null) 
         { 
          var htmlWithImages = content.Value.Replace(result, string.Format("img src='{0}' class='newsimage' width='300'", firstOrDefault.ImageUrlId)); 
          node.Content = htmlWithImages; 
         } 
        } 

但是,我的代码是错误的,因为如果有不止一个匹配它仅替换最后一个,我怎么能纠正我的替换所有匹配文本代码?

+0

什么是'content'类型?它在哪里宣布?节点的类型是什么?它在哪里宣布? –

回答

3

你是丢失for循环体的花括号。如果没有花括号,多次执行的唯一行是第一行。

试试这个:

foreach (Match match in matches) 
{         // added curly brace here 
    result = match.Value; 

    if (result != null) 
    { 
     var firstOrDefault = node.ListImages.FirstOrDefault(); 
     if (firstOrDefault != null) 
     { 
      var htmlWithImages = content.Value.Replace(result, 
       string.Format("img src='{0}' class='newsimage' width='300'", 
           firstOrDefault.ImageUrlId)); 
      node.Content = htmlWithImages; 
     } 
    } 
}         // added curly brace here 

我也想进一步补充两点:

  • 有一个名为Regex.Replace方法,您可以使用,而不是先找到你想要的字符串使用正则表达式替换,然后使用string.Replace
  • 如果您尝试解析HTML,最好使用HTML解析器。看看HTML Agility Pack,看看它是否可以更简单地解决您的问题。
+0

谢谢,无论如何主要的问题是:在第一次我替换content.Value第一次匹配和然后我替换content.Value第二次匹配,并且第一次替换不保存 – revolutionkpi

+0

@revolutionkpi:关于您的代码的一个奇怪的事情是您正在从'content.Value'中读取并指定给'node.Content'。你确定当你赋值给'node.Content'时,这些改变在'content.Value'中是可见的吗?我认为从文档中读取内容并转换为纯字符串会更有意义,可以对字符串进行所需的替换('s = s.Replace(...);'),然后重新分配最终结果到最后的文档(不在循环中)。但是你真的应该使用HTML敏捷包来处理这类事情。它会让你的生活更轻松。 –

+1

@revolutionkpi:顺便说一下,我的回答实际上是针对你的“主要”问题。如果您认为我的回答没有回答您的问题,那么我认为您可能会误读或误解了我的答案。您的代码中还可能没有*一个*,但是*多个*错误。那么我建议你一个接一个地修复它们,并且不要气馁,修复一个错误并不能立即解决你所有的问题。有时你需要采取很多小步骤才能到达最终目的地。 –

1

我想你可能会缺少一组围绕你的循环括号...

只有这条线被循环。这就是为什么你的代码只更新中的最后一项,作为结果被设置到最后一个项目集合中(关于在foreach的最后一次迭代)

  foreach (Match match in matches) 
         result = match.Value; 

更正代码

Regex imgRule = new Regex("img id=\\\".+?\\\""); 
         MatchCollection matches = imgRule.Matches(content.Value); 
         string result = null; 
         foreach (Match match in matches) { 
          result = match.Value; 

          if (result != null) 
          { 
           var firstOrDefault = node.ListImages.FirstOrDefault(); 
           if (firstOrDefault != null) 
           { 
            var htmlWithImages = content.Value.Replace(result, string.Format("img src='{0}' class='newsimage' width='300'", firstOrDefault.ImageUrlId)); 
            node.Content = htmlWithImages; 
           } 
          } 
         } 
+0

谢谢,但是当我用第二个匹配替换字符串时,第一个是没有保存,所以在结果中只有最后一个替换 – revolutionkpi

1
 
foreach (Match match in matches) 
{ 
    result = match.Value; 

    if (result != null) 
    { 
     var firstOrDefault = node.ListImages.FirstOrDefault(); 
     if (firstOrDefault != null) 
     { 
      var htmlWithImages = content.Value.Replace(result, string.Format("img src='{0}' class='newsimage' width='300'", firstOrDefault.ImageUrlId)); 
      node.Content = htmlWithImages; 
     } 
    } 
}