Assuming there are no nested paragraphs...
my $to_insert = get_thing_to_insert();
$text =~ s/((?:<p>.*?</p>|\n\n){2})/$1$to_insert/s;
should just about do it.
With extended formatting:
$text =~ s{
( # a group
(?: # containing ...
<p> # the start of a paragraph
.*? # to...
</p> # its closing tag
| # OR...
\n\n # two newlines alone.
){2} # twice
) # and take all of that...
}
{$1$to_insert}xms # and append $val to it
Note, I used \n\n as the delimiter; if you're using a windows style text file, this needs to be \r\n\r\n
, or if it might be mixed, something like \r?\n\r?\n
to make the \r
optional.
Also note that because the '\n\n' is after the |
, the <p>
blocks can have double newlines in them - <p>
to </p>
takes priority. If you want newlines inside the <p>
's to take priority, swap those around.