Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex: Remove more than 2 consecutive <br>?

In a string, I would like to replace any occurrences of:

<br /><br /><br /> (or more) 

with:

<br /><br />

This has worked for replacing 2 or more consecutive occurrences of the letter e with an asterisk:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Dim strInput As String = "Thee brown fox jumped over the lazy dog."
Label1.Text = Regex.Replace(strInput, "e{2,}", "*")

Giving:

Th* brown fox jumped over the lazy dog.

These regex haven’t worked for replacing two or more consecutive occurrences of the HTML breaks with an asterisk:

Dim strInput As String = "Thee <br /><br /><br /> brown fox jumped over the lazy dog."
Label1.Text = Regex.Replace(strInput, "\b<br />{2,}\b", "*")
Label1.Text = Regex.Replace(strInput, "\b<br />\b{2,}", "*")
Label1.Text = Regex.Replace(strInput, "\b<br />\b {2,}", "*")

Can anyone please give a syntax that will work?

(I could temporarily swap out all occurrences of the HTML breaks in the string with say [HTML-Linebreak] before executing the code if that would be easier. Then I could replace them back after.)

>Solution :

Try it, it worded with me: Regex.Replace(strInput, "(<br\s*/?>\s*){3,}", "<br /><br />")

    Dim strInput As String = "Thee <br /><br /><br /><br /><br /><br /> brown fox jumped over the lazy dog."
    Console.WriteLine(Regex.Replace(strInput, "(<br\s*/?>\s*){3,}", "<br /><br />"))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading