Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Question: Capturing a repeated pattern

by rubasov (Friar)
on Apr 08, 2010 at 22:05 UTC ( #833647=note: print w/ replies, xml ) Need Help??


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;


Comment on Re: Question: Capturing a repeated pattern
Select or Download Code
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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://833647]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (9)
As of 2014-04-19 09:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (478 votes), past polls