Reputation: 2583
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 />");
Upvotes: 4
Views: 944
Reputation: 424983
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 />
Upvotes: 3
Reputation: 41
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();
}
Upvotes: 1