in reply to A query on greedy regexps

Well, you regex is not working, at least not as I'd expect it to work - apart from the fact that the 'Class\n====\n' header is missing. The array @devices gets just the first entry line ('graphics 0 ...'). The reason for this is that the /g modifier on the regex is not matching multiple times because the header is there only once.

So do it in two steps like this:

#!/usr/bin/perl $_ = do {local $/; <DATA>}; s/(Class.+\n=+\n)//; $header = $1; @devices = m/(^\w.+\n(?:\s+.+\n)*)/mg; __DATA__ Class Foo ========= graphics 0 1 graph3 CLAIMED INTERFACE Graphics disk 0 2/0/1.0.0 sdisk CLAIMED DEVICE HP +C2247M1 /dev/dsk/c0t0d0 /dev/rdsk/c0t0d0 ctl 0 2/0/1.7.0 sctl CLAIMED DEVICE Initiato +r /dev/rscsi/c0t7d0
as far as I can tell this is not a problem with greedy matching. You just expected the /g modifier to dwim more than it does.

-- Hofmator

Replies are listed 'Best First'.
Re: Re: A query on greedy regexps
by sch (Pilgrim) on Oct 01, 2002 at 15:31 UTC

    Oops, you are of course right - just re-ran the code, and it finds the first element but that's it

    I actually had your suggestion originally (more or less) and just dropped it down to one regex and it seemed to work - yet another perfect example of why I shouldn't make assumptions about what seems simple code.