#!/usr/bin/perl
use strict;
use warnings;
my $text = <
This is a pre with a break.
This is a line with a break.
EOT 1 while $text =~ s{^((?:(?!).|)*?)\n}{$1(?:(?!).)*
##
This is a line
with a break.
This is a pre
with a break.
This is a line
with a break.
####
The regular expression:
(?is-mx:^((?:(?!).|(?:(?!
).)*
)*?)\n)
matches as follows:
NODE EXPLANATION
----------------------------------------------------------------------
(?is-mx: group, but do not capture (case-insensitive)
(with . matching \n) (with ^ and $ matching
normally) (matching whitespace and #
normally):
----------------------------------------------------------------------
^ the beginning of the string
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the least amount
possible)):
----------------------------------------------------------------------
(?! look ahead to see if there is not:
----------------------------------------------------------------------
''
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
. any character
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
''
----------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the most amount
possible)):
----------------------------------------------------------------------
(?! look ahead to see if there is not:
----------------------------------------------------------------------
'
'
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
. any character
----------------------------------------------------------------------
)* end of grouping
----------------------------------------------------------------------
'
'
----------------------------------------------------------------------
)*? end of grouping
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
\n '\n' (newline)
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------