Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I am familar with pattern matching but not sure how to tackle the following. I have a single line with numerous entries req=XX, where XX is a number.
req=44...something...req=56...req=24
i need to extract each of numbers and print to a new file. I have tried the below put this only finds the first occurance then dies?
while (<>) { if (m/req\=(\d+){1,400}/) {print OUT "$1\n"}; }

Replies are listed 'Best First'.
Re: pattern matching
by ikegami (Patriarch) on Jun 27, 2005 at 14:43 UTC

    You need to use /g:

    while (/req=(\d+)/g) { print OUT ("$1\n"); }
      thanks for that, works fine cheers for the quick reponse
Re: pattern matching
by Ido (Hermit) on Jun 27, 2005 at 14:45 UTC
    You should look at the /g modifier of the pattern matching operators:
    while(m/.../g){ ...# Every match }


    BTW: What do you mean by (\d+){1,400}? It's something like "One or more digit, 1-400 times..", which makes the {1,400} somewhat redundant.
Re: pattern matching
by JediWizard (Deacon) on Jun 27, 2005 at 14:48 UTC

    To expand on other's answers with a link to the documentation: See perlop (look at the "Regexp Quote-Like Operators" section).


    They say that time changes things, but you actually have to change them yourself.

    —Andy Warhol

Re: pattern matching
by GrandFather (Saint) on Jun 28, 2005 at 05:28 UTC

    How about a "one liner":

    use strict; print join ("\n", /req=(\d+)/g) . "\n" while <DATA>; __DATA__ req=44...something...req=56...req=24 req=23 diddle diddle diddle req=1 req=2

    Perl is Huffman encoded by design.