in reply to multiple-line match mucking with regex registers

You could do it this way: (Basically merging the two regexes so that only two sets of capture brackets appear.)

#! perl -slw use strict; my @lines = ( '111 MatchMe [This is why]', '222 I need to be dispatched to the same callback: For This Reason', ); for ( @lines ) { m/ ^(\d+) \s (?: (?-x:I need to be dispatched.*?: ) | (?-x:MatchMe \[) ) (.*?) \]? $ /x and print "'$1'$2'"; } __END__ C:\test>junk '111'This is why' '222'For This Reason'

Though you will have to decide whether the possibility of obtaining a false matche against lines like update: these

111 MatchMe [This is why 222 I need to be dispatched to the same callback: For This Reason]

is a problem for your application?


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: multiple-line match mucking with regex registers
by GrandFather (Saint) on Jul 11, 2006 at 17:59 UTC

    If I'd thought ahead I'd have used a lookahead like this:

    use warnings; use strict; while (<DATA>) { chomp; next if ! /^ (\d+)\s # Capture the number (?: # Start of non-capture group MatchMe\s\[(?=.*\]$) # Match matchme [.*] | I\sneed\sto\sbe\sdispatched.*\:\s(?=[^\]]*$) ) (.*?)\]?$/x; #capture reason print "Line $1, reason: $2\n"; } __DATA__ 111 MatchMe [This is why] 222 I need to be dispatched to the same callback: For This Reason 112 MatchMe [This is why 223 I need to be dispatched to the same callback: For This Reason]

    OP should note the positive lookahead asertions (?=...) that prevent the false matches found by the simpler expression. Prints:

    Line 111, reason: This is why Line 222, reason: For This Reason

    DWIM is Perl's answer to Gödel
Re^2: multiple-line match mucking with regex registers
by Voronich (Hermit) on Jul 11, 2006 at 14:28 UTC
    Ugh. Ya've gotta be so careful with terms when talking about regexes. By "multi-line matching" I didn't mean a match that spans across two lines of data. I meant an expression that would match different 'types' of lines. My apologies for the confusion. That's what I get for trying to put all the particulars in the subject.

    So no, that kind of false positive is not an issue. The lines (log files) I'm matching against are pretty strongly formatted. Plus, these are remarkably oversimplified examples, so that kind of clash would be very very tough to conjure.