I’m trying replace two consecutive line breaks with the HTML tag <p/>. So in a string such as:
\r\n\r\n\r\n
There are two consecutive occurrences of \r\n\r\n,
The result should be:
<p/><p/>
but with C# String.Replace, it only detects the first occurrence and I just get back:
<p/>\r\n
So I’m wondering if any regular expression gurus know how to detect that using regular expression?
Edit:
I figured the question is a bit confusing. Let me rephrase it. The requirement should be to replace any "\r\n" with a tag <p/> only if there is another “\r\n” immediately before it.
Such with the string:
\r\n\r\n\r\n
- The first
\r\n, does not have another\r\nbefore it, nothing should be done, - The second
\r\n, it does have another\r\nbefore it, qualifies for replacement, - The third
\r\n, it does have another\r\nbefore it, also qualifies for replace.
So the result should be:
<p/><p/>
Yes, you can do this with a regular expression:
If performance is an issue then you might find that rebuilding the string manually is quicker than a regex, but the drawback will be the more complicated code. (I’d probably go for the regex in 99% of situations.)