Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Evaluating code in a regex at runtime: (??{...})

by Athanasius (Abbot)
on Jan 28, 2013 at 02:33 UTC ( #1015619=note: print w/ replies, xml ) Need Help??


in reply to Evaluating code in a regex at runtime: (??{...})

Note that the contents of the match are still accessible via a named capture group:

#! perl use Modern::Perl; ('a' x 100) =~ /(?<match>(??{'(.)' x 100}))/; say $+{match};

Output:

12:28 >perl 504_SoPW.pl aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 12:28 >

See Capture groups.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


Comment on Re: Evaluating code in a regex at runtime: (??{...})
Select or Download Code
Re^2: Evaluating code in a regex at runtime: (??{...})
by LanX (Canon) on Jan 28, 2013 at 07:34 UTC
    > Note that the contents of the match are still accessible via a named capture group:

    > ...  ('a' x 100) =~ /(?<match>(??{'(.)' x 100}))/;

    ehm ... what you're showing also works with unnamed capture groups:

    DB<113> ('a' x 10) =~ /((??{'(.)' x 10}))/; print "<$1>" => 1 <aaaaaaaaaa>

    The OP's point was about dynamically generated capture groups, but yours is outside the embedded Perl code.

    Cheers Rolf

      Yes, you’re right. So in dave_the_m’s example, all we need to do is add another set of parentheses:

      /(....)...(...)...((??{foo()}))....(...)/ ^ ^ ^ ^ 1 2 3 4

      and the dynamically generated capture group is assigned to $3. For example:

      use Modern::Perl; my $str = 'abcd$$$$$ef'; my @caps = $str =~ /(a)b(c)d((??{'(.)' x 5}))e(f)/; say join(', ', @caps);

      Output:

      18:33 >perl 504_SoPW.pl a, c, $$$$$, f 18:38 >

      What we (apparently) can’t get is multiple capture groups matches assigned to different capture groups (whether named or unnamed) from the same dynamic match. Correct?

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

        > What we (apparently) canít get is multiple capture groups assigned to different capture groups (whether named or unnamed) from the same dynamic match. Correct?

        apparently yes, like documented:

        DB<131> 'aaa' =~ /(?<outer>(??{'(?<inner>.)' x 1}))/; keys %+ => "outer"

        But of course there are workarounds ... if necessary.

        Cheers Rolf

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1015619]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (19)
As of 2015-07-01 15:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (6 votes), past polls