Regular expression to get the third part of a paragraph in HTML

0

I created this regex and apparently it's almost the way I would like it, however, I'd like to insert the <img> tag after the third occurrence of a sequence of paragraphs. The way I did it is inserting <img> after the first paragraph of all occurrences.

That is, every third occurrence of a paragraph <p> , I would like to insert a <img> tag.

(<p(.*?)>)([[:space:]]+?|.*?|[[:space:]]+?)(<\/p>)

Here is the example I made: link

Example of how my regex was:

Data entry:

<p>texto1</p>
<p>texto1</p>
<p>texto1</p>

Result:

<p>texto1</p>
   <img src="">
<p>texto1</p>
   <img src="">
<p>texto1</p>
   <img src="">

Here's how I'd like to:

<p>texto1</p>
<p>texto1</p>
<p>texto1</p>
   <img src="">

<p>texto1</p>
<p>texto1</p>
<p>texto1</p>
   <img src="">
    
asked by anonymous 06.12.2018 / 17:59

1 answer

0

Based on what you had done, I formulated this regex:

((<p(.*?)>)([[:space:]]+?|.*?|[[:space:]]+?)(<\/p>)\s?){3}

The only changes I made was to add everything in a single group and expect it to have 3 occurrences ( {3} ).

  

See working at Regex101

    
06.12.2018 / 18:35