in reply to PCRE: Repeating Subpattens After Intervening Characters

Oops. You're looking for PCREmonks, not Perlmonks. There are definitely Perl answers to your question, but PCRE isn't Perl, and Perl isn't PCRE, so no answers will be directly applicable.

At least you were nice enough to announce this as a PCRE problem. We've had past inquisitors ask about PHP and Java regexen without prior disclosure, which only leads to broken useless answers because we (sensibly) presume a Perl environment.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

  • Comment on Re: PCRE: Repeating Subpattens After Intervening Characters

Replies are listed 'Best First'.
Re^2: PCRE: Repeating Subpattens After Intervening Characters
by QM (Parson) on Sep 14, 2005 at 22:03 UTC
    Oops. You're looking for PCREmonks, not Perlmonks.
    Darn. I knew it was too easy to reply with:
    my $regex = qr/(\d\s+foo)/; $some_string =~ /$regex[^\r\n]*$regex/;

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

      That doesn't capture properly. Fix:
      my $regex = qr/\d\s+foo/; $some_string =~ /($regex)[^\r\n]*($regex)/;

      Also, your solution doesn't scale (if capturing is desired). He can't say, for example, the following while capturing what $regexp matches:

      my $regex = qr/\d\s+foo/; $some_string =~ /(?:$regex[^\r\n]*)*($regex)/;
        I'm confused -- in what sense doesn't it capture properly?
        DB<1> $x = qr/(\d\s+foo)/; DB<2> $y = 'alpha beta 1 foo gamma delta 2 foo epsilon omega'; DB<3> @z = $y =~ /$x[^\r\n]*$x/; DB<4> x @z 0 '1 foo' 1 '2 foo' DB<5>
        However, I can vaguely see problems getting the capturing desired, if it isn't conveniently what happens when trying to scale.

        -QM
        --
        Quantum Mechanics: The dreams stuff is made of

      Oh...I didn't realize PCREmonks existed. I'll go ask them. Thanks for being friendly in the meantime. :-)

      Alex