Replace line breaks except for ones between
    
                                                      
                                                              
                                                                      
                                
                                                                                                                                    





                            

2019-06-28 02:16发布

I'm looking to replace/remove all line breaks within a given string except for ones nested within a <pre> tag. So for the following string:

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

I would like to remove all line breaks except for ones nested within a pre tag:

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

2条回答
Animai°情兽
2楼-- · 2019-06-28 02:29

Use a negative look ahead and you can still do it in one line:

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

Here's some test code, with a better sample containing multiple <pre> tags:

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

Output:

<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 />  
查看更多
不美不萌又怎样
3楼-- · 2019-06-28 02:42

Is not beautiful, but works for me.

    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();
        }
查看更多
登录 后发表回答