meghana
meghana

Reputation: 907

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

Upvotes: 1

Views: 3271

Answers (2)

Alan G&#243;mez
Alan G&#243;mez

Reputation: 378

An option could be:

:%s/<p>[^<]\+<\/p>\zs\|\(<p>.\{-}[^<]\+\)/\1<\/p>/g
:%s/<\/p><\/p>/<\/p>/g

Input:

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

Output:

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

Upvotes: 0

Oded
Oded

Reputation: 499382

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.

Upvotes: 6

Related Questions