J
jl_post
Hi,
I have some code that splits text right after a line containing
only a single dot. To do this, I used a positive look-behind
assertion, like in this sample script:
#!/usr/bin/perl
use strict;
use warnings;
my $text = <<"END_OF_TEXT";
Line 1
..
Line 2
..
Line 3
END_OF_TEXT
# Use a positive look-behind assertion
# to split $text right after a dot on
# a line by itself:
my @elements = split m/(?<=\n\.\n)/, $text;
use Data:umper;
print Dumper @elements;
__END__
Running this program gives the output:
$VAR1 = 'Line 1
..
';
$VAR2 = 'Line 2
..
';
$VAR3 = 'Line 3
';
Basically, $text was split into three elements, which each element
(except for the last) ending with a dot (on a line by itself).
This positive look-behind assertion works great if the dot is truly
on a line by itself. But it there was leading and/or trailing
whitespace with the dot (on a line by itself), the regular expression
m/(?<=\n\.\n)/ won't split after that line.
What I'm looking for is to do something like this:
#!/usr/bin/perl
use strict;
use warnings;
my $text = <<"END_OF_TEXT";
Line 1
I have some code that splits text right after a line containing
only a single dot. To do this, I used a positive look-behind
assertion, like in this sample script:
#!/usr/bin/perl
use strict;
use warnings;
my $text = <<"END_OF_TEXT";
Line 1
..
Line 2
..
Line 3
END_OF_TEXT
# Use a positive look-behind assertion
# to split $text right after a dot on
# a line by itself:
my @elements = split m/(?<=\n\.\n)/, $text;
use Data:umper;
print Dumper @elements;
__END__
Running this program gives the output:
$VAR1 = 'Line 1
..
';
$VAR2 = 'Line 2
..
';
$VAR3 = 'Line 3
';
Basically, $text was split into three elements, which each element
(except for the last) ending with a dot (on a line by itself).
This positive look-behind assertion works great if the dot is truly
on a line by itself. But it there was leading and/or trailing
whitespace with the dot (on a line by itself), the regular expression
m/(?<=\n\.\n)/ won't split after that line.
What I'm looking for is to do something like this:
#!/usr/bin/perl
use strict;
use warnings;
my $text = <<"END_OF_TEXT";
Line 1