my $str = " Hi Some other lines of stuff "; $str =~ s{ ^(.+)$ (??{ lc(substr $^N, 0, 7) eq "" ? '(?!)' : '(?=)' }) }{<para>$1</para>}xgm; print $str; # prints <para><html></para> <title>Hi Some other lines of stuff