http://qs321.pair.com?node_id=1230882


in reply to Regex backreference without capture

Hi

? So you want  @x = qw( first third ) ;

This "works"

dd( 'bogus firstblahjokeblahthird bogus' =~ m/ (first) (?&patblah) .*? (?&patblah) (third) (?(DEFINE) (?<patblah>blah) ) /sx ); __END__ ("first", "third", undef)

The undef comes from the named capture  (?<patblah>blah) which takes up slot $3 in this pattern, which is why (?(DEFINE) is placed at the end

Replies are listed 'Best First'.
Re^2: Regex backreference without capture (?&NAME) NamedCapture
by QM (Parson) on Mar 05, 2019 at 10:33 UTC
    Thanks.

    But does this do backreferences? I see it matches the same pattern, but that's not the same as a backreference.

    But thanks, that makes me think about the problem differently.

    Edited to add:
    ------------------
    I'm considering using named capture groups, where there's a need to have a well-defined return value. Which is something PCRE has, and some people (!) can't live without, apparently.

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

Re^2: Regex backreference without capture (?&NAME) NamedCapture
by rsFalse (Chaplain) on Mar 05, 2019 at 12:34 UTC
    E.g. the input is "bogus firstblAHj?keblOWthird bogus", and pattern is 'bl..', then it matches both, but OP would like to match only the same :)