you mean "easier to maintain" and "more readable" :o) ---- though i would like to find a multiple capturing regex parens as a general problem, so i am exploiting the thread to ask this question i.e. how to solve this with a multiple match and not split . timtowtdi
2008/12/24 Peter Gordon <[email protected]>: > Cute! > No repetition, and shorter > > On Wed, 2008-12-24 at 17:23 +0200, Avishalom Shalit wrote: >> @b=split(/\b(?=Title)/,$text); >> that should do it, >> split on the word boundary right vefore the Title >> >> 2008/12/24 Shlomi Fish <[email protected]>: >> > 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 >> > >> >> >> > > _______________________________________________ > Perl mailing list > [email protected] > http://perl.org.il/mailman/listinfo/perl > -- -- vish _______________________________________________ Perl mailing list [email protected] http://perl.org.il/mailman/listinfo/perl
