Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Welcome to the Monastery
 
PerlMonks  

Multiple newline regex

by rhythmicus (Sexton)
on Aug 31, 2004 at 14:05 UTC ( [id://387271]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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

Here's the test case:

while (<DATA>) { if (/(\d)\n{2}/) { print "$1\n"; } } __DATA__ Line 1 Line 2 Line 3 Line 5 Line 6 Line 7 Line 9

My question is, why does this not print:

3 7

As it stands, the script prints nothing. I know there are no \r or \f in the data, and just to be sure I tried matching against \W{2,}.

What am I missing here?

Replies are listed 'Best First'.
Re: Multiple newline regex
by Roy Johnson (Monsignor) on Aug 31, 2004 at 14:18 UTC
    You're reading one line at a time.

    Update: here's a fix that reads in paragraph mode. I think this is what you want.

    $/=''; while (<DATA>) { print "$1\n" if /(\d)\n\n/; }

    Caution: Contents may have been coded under pressure.
Re: Multiple newline regex
by ikegami (Patriarch) on Aug 31, 2004 at 14:18 UTC

    Because you're matching against $_, which only contains a single line of __DATA__ at any given time. Here's a fix:

    { # Read all of __DATA__ into $_: local $/; $_ = <DATA>; } while (/(\d)\n{2}/g) { print "$1\n"; } __DATA__ Line 1 Line 2 Line 3 Line 5 Line 6 Line 7 Line 9
Re: Multiple newline regex
by Fletch (Bishop) on Aug 31, 2004 at 14:19 UTC

    Presuming the default value of $/ then at any time <DATA> is going to have returned after encountering the first newline. The next read will produce a string containing just a single "\n". At no time will $_ ever hold more than a single newline.

Re: Multiple newline regex
by ysth (Canon) on Aug 31, 2004 at 14:19 UTC
    You are only reading one line at a time, so you are never going to be able to match against \n{2}. See perlvar on $/ about paragraph mode ($/ = "") or slurp the whole file and loop with //g.
Re: Multiple newline regex
by rhythmicus (Sexton) on Aug 31, 2004 at 14:27 UTC

    Doh! I knew I was missing something very obvious.

    I've been away from Perl for a while, and I'm just getting back into the swing of things. Thanks for the reminder.

Re: Multiple newline regex
by Velaki (Chaplain) on Aug 31, 2004 at 14:36 UTC

    By default, you're reading only one line at a time, so you'll never match something across two lines.

    Simply prepend your code with

    $/=""; # slurp data in paragraph mode, see perlvar for more info
    and you'll be able to use your existing code "unaltered".

    You can do a chock full of interesting things with $/. Take a gander at perlvar and play with the different ways you can read data.

    Cheers!
    -v
    "Perl. There is no substitute."

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://387271]
Approved by ikegami
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.