#!/usr/bin/perl use strict; use warnings; $_ = join '', ; s/(

(?:(?!<\/p>).)*<\/p>)(??{-1 == index $1, "\n\n" and '(*FAIL)'})/-->$1<--/gs; print; __DATA__ a 123 456 789 b 123 456 789 c 123 456 789 d 123 456 789

e 123 456 789 f 123 456 789

e 123 456 789 f 123 456 789 g 123 456 789 h 123 456 789

1 123 456 789 2 123 456 789 3 123 456 789 4 123 456 789