On Wednesday 24 December 2008, Peter Gordon wrote:
> Suppose I have the following code
> my $text =<<EOF;
> Title1
> text1
> text2
> Title2
> text3
> text4
> EOF
>
> @blocks = $text =~ m!(^Title\S+.*?)(?=^Title|\Z)!mgs ;
>
> The idea is to split the text into blocks. This code works, but the
> lookahead requires repeating part of the first half of the regex.
>
> Is there a shorter regex, without the repetition, that can do the same
> thing?
Don't know about shorter, but you can extract the common string to a variable:
{{{{{{{{{{{{{{{{
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my $text =<<'EOF';
Title1
text1
text2
Title2
text3
text4
EOF
my $delim = "^Title";
my @blocks = $text =~ m!($delim\S+.*?)(?=$delim|\Z)!gms ;
print Dumper([...@blocks]);
}}}}}}}}}}}}}}}}
A few notes:
1. You should always specify the delimiters of the here-doc explicitly. People
don't know off-hand if it's <<'EOF' or <<"EOF".
2. Someone I know recommends to sort the option characters for a Perl regex
alphabetically - like "gms" instead of "mgs". This facilitates searching for
them.
Regards,
Shlomi Fish
>
> Peter
>
> _______________________________________________
> Perl mailing list
> [email protected]
> http://perl.org.il/mailman/listinfo/perl
--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
My Aphorisms - http://www.shlomifish.org/humour.html
Shlomi, so what are you working on? Working on a new wiki about unit testing
fortunes in freecell? -- Ran Eilam
_______________________________________________
Perl mailing list
[email protected]
http://perl.org.il/mailman/listinfo/perl