in reply to How to capture quantified repeats?

How about using variables to turn confusion to clarity? Better yet, go to the library and borrow Perl Best practices to see why '\A' is better than \$'. spreading out your regex and adding comments, helps, too.

my $ham = "spam\tspam\tspam\tyam\tclam"; my $word = qr{[^\t]+}; my $sep = qr{\t}; my $capture = qr{($word)}; my (@jam ) = ($ham =~ m{\A # enforce beginning of stri +ng $word $sep # skip first word and separ +ator $word $sep # and the second $capture $sep # capture next two words $capture $sep # skip the separators $word # skip a word \z # and then it's the end of +the string }xms); print join("\n", '**', @jam, '**', '');

Using the debugger helps, too ... Along the way I noticed your string has a double tab '\t\t', but you only ever accept single tabs; You specify end of string '$', when there's still another word to go.

But you're doing too much work. You could split() on '\t' and select only the components you want. If it's the 3rd % 4th ...

my @jam = ( spit "\t", $ham )[2,3];

If you do need to use a regex, do you need to check whether there is a word after your capture? Do you need to enforce there is nothing after that last word? Simplify your regex for greater happiness.

As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Replies are listed 'Best First'.
Re^2: How to capture quantified repeats?
by DonJaime (Initiate) on Sep 22, 2010 at 20:49 UTC

    Something like:

    my $ham = "spam\tspam\tspam\t\tyam\tclam"; my @jam; $ham =~ s/^[^\t]*\t[^\t]*((?:\t[^\t]*){3})\t[^\t]*$/push @jam,(split " +\t",$1);$1/eg; print join("\n", '**', @jam, '**', '');

    maybe?