Search code examples
asp.netregex

Regex to find unclosed HTML tags and close them


I need a regular expression , which gives me unclosed tags , and i can find them and close them programmatically.

Like , i have below text

<tag>
<p> hello world <p> this is <p>test.</p> this is test. <p> end it 
</tag>

i want to find unclosed tags from this and close them using Regex.

any idea?? Thanks Meghana


Solution

  • Regex is not a suitable tool for this task. See here for a compelling demonstration of why.

    I suggest you use the HTML Agility Pack to parse and rewrite the HTML.