http://www.perlmonks.org?node_id=833647


in reply to Question: Capturing a repeated pattern

Probably I do not know your specific problem, but your regex seems to be an overkill, a simple split is not sufficient for you?
my $str = 'somename 1000 0.24 280 2 2576.9 2731.9 12.0 4195.3' +; my @array = split /\s+/, $str;

update: It seems to me you have two goals: to verify the format of your input and to split it to fields. I think it is much more readable in two separate steps:

die "invalid input format: '$str'" if $str !~ / \A [a-z]\w* (?: \s+ [\d.]+ ){8} \z /ixo; my @array = split /\s+/, $str;

Replies are listed 'Best First'.
Re^2: Question: Capturing a repeated pattern
by robmderrick (Initiate) on Apr 08, 2010 at 22:19 UTC
    A split for that input line alone would work perfectly. But, my input is quite varied, and for this purpose, I only want to match those lines that exactly match my pattern, and not all of the others that split would grab.

      You could always match first and then split:

      $s = 'somename 1000 0.24 280 2 2576.9 2731.9 12.0 4195.3';; $s =~ m[\w+(?:\s+(?:\d+\.)?\d+){8}] and @a = split ' ', $s and shift @a and print "@a";; 1000 0.24 280 2 2576.9 2731.9 12.0 4195.3

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
      Sorry, I've updated the grandparent node before I saw your answer. And of course if the separator part cannot be easily subtracted from your regex then split won't suffice and you will need what kennethk recommended.

        Please indicate in the node that you have updated it otherwise future readers of the thread will be left somewhat bemused by the disconnect between what is written and the reply it seemingly received.

        True laziness is hard work