[Israel.pm] Regex question

Peter Gordon peter at pg-consultants.com
Wed Dec 24 06:55:43 PST 2008


a) It's longer code
b) the title gets lost

But thanks all the same.


On Wed, 2008-12-24 at 15:32 +0200, bc.other wrote:
> #!/usr/bin/perl
> 
> my $block = <<EOF;
> Title1
>   text1
>   text2
> Title2
>   text3
>   text4
> EOF
> 
> my @sp = split(/Title\d/, $block);
> foreach my $i (@sp)
> {
>    chomp($i);
>    print "$i\n-------------------";
> }
> 
> 
> 
> -----Original Message-----
> From: perl-bounces at perl.org.il [mailto:perl-bounces at perl.org.il] On Behalf
> Of Peter Gordon
> Sent: Wednesday, December 24, 2008 1:41 PM
> To: Perl in Israel
> Subject: [Israel.pm] Regex question
> 
> 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?
> 
> Peter
> 
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://perl.org.il/mailman/listinfo/perl
> 
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://perl.org.il/mailman/listinfo/perl




More information about the Perl mailing list