in reply to Question: Capturing a repeated pattern

The issue is that if the parentheses only appear once in the regular expression and you use a repetition to represent your match like (\d){8}, you will repeatedly overwrite the same buffer. Probably the clearest solution is using the repetition operator x in constructing the string for your regular expression:

#!/usr/bin/perl use strict; use warnings; $_ = 'somename 1000 0.24 280 2 2576.9 2731.9 12.0 4195.3'; my $regex = '(^[a-z]\w*)' . '\s+([\.\d]+)' x 8 . '$'; my (@array) = /$regex/io; print join "\n", @array; __END__ somename 1000 0.24 280 2 2576.9 2731.9 12.0 4195.3

Note as well that if you are operating on the special variable $_ there is no need to bind it to the regular expression.

You can also construct that in the regular expression itself using A bit of magic: executing Perl code in a regular expression, as described in perlretut.

Replies are listed 'Best First'.
Re^2: Question: Capturing a repeated pattern
by robmderrick (Initiate) on Apr 08, 2010 at 23:03 UTC
    Thanks Kenneth, and all the other responders. I just wanted to make sure that my suspicion that there was an easy way to do this that wasn't long and ugly, but also not profoundly obfuscated was correct, or not correct. Like the "just use a split" answer, which if the input was uniform, would of course been the correct answer. Thanks all, I've got what I need. -- rob