【问题标题】:Replace line breaks except for ones between <pre> tags替换除 <pre> 标签之间的换行符
【发布时间】:2014-01-17 14:27:06
【问题描述】:

我希望替换/删除给定字符串中的所有换行符,但嵌套在&lt;pre&gt; 标记中的换行符除外。所以对于以下字符串:

var text = @"
    Some contents which is formatted
    over multiple
    lines but contains a 
    <pre>
        tag which has
        also has
        multiple line breaks.
    </pre>
";

我想删除所有换行符,除了嵌套在 pre 标记中的换行符:

Regex.Replace(text, "\n", "<br />");

【问题讨论】:

    标签: c# html regex replace


    【解决方案1】:

    使用消极的前瞻性,你仍然可以在一行中做到:

    text = Regex.Replace(text, "\n(?![^<]*</pre>)", "<br />");
    

    这里有一些测试代码,更好的示例包含多个&lt;pre&gt; 标签:

    var text = @"
        Some contents which is formatted
        over multiple
        lines but contains a 
        <pre>
            tag which has
            also has
            multiple line breaks.
        </pre>
        foo 1
        bar 1
        <pre>
            tag which has
            also has
            multiple line breaks.
        </pre>
        foo 2
        bar 2
    ";
    text = Regex.Replace(text, "\n(?![^<]*</pre>)", "<br />");
    Console.WriteLine(text);
    

    输出:

    <br />    Some contents which is formatted<br />    over multiple<br />    lines but contains a <br />    <pre>
        tag which has
        also has
        multiple line breaks.
    </pre><br />    foo 1<br />    bar 1<br />    <pre>
        tag which has
        also has
        multiple line breaks.
    </pre><br />    foo 2<br />    bar 2<br />  
    

    【讨论】:

      【解决方案2】:

      不漂亮,但对我有用。

          static void Main(string[] args)
              {
                  var text = @"
          Some contents which is formatted
          over multiple
          lines but contains a 
          <pre>
              tag which has
              also has
              multiple line breaks.
          </pre>
      ";
                  int pre_tag_ini = text.IndexOf("<pre>");
                  int pre_tag_fim = text.IndexOf("</pre>");
                  string result = Regex.Replace(text.Substring(0, pre_tag_ini), "\r\n", "<br />");
                  result += text.Substring(pre_tag_ini, pre_tag_fim - pre_tag_ini);;
                  result += Regex.Replace(text.Substring(pre_tag_fim, text.Length - pre_tag_fim), "\r\n", "<br />");
      
                  Console.Write(result);
                  Console.ReadLine();
              }
      

      【讨论】:

      • 如果您将文本视为 HTML 并使用 HTML Agility Pack,可能会有一种优雅的方式来执行此操作,但对于给定的内容,这是可行的。
      猜你喜欢
      • 1970-01-01
      • 2013-11-01
      • 2010-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多