替换除<pre> tags</pre>之间的换行符

时间:2014-01-17 14:27:06

标签: c# html regex replace

我希望替换/删除给定字符串中的所有换行符,除了嵌套在<pre>标记内的换行符。所以对于以下字符串:

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 />");

2 个答案:

答案 0 :(得分:3)

使用负向前看,你仍然可以在一行中完成:

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

以下是一些测试代码,其中包含多个<pre>标记的更好示例:

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 />  

答案 1 :(得分:1)

不漂亮,但适合我。

    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();
        }