Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Code expressions in regex refer to the regex group captured, by name / relative

by Anonymous Monk
on Jan 14, 2022 at 00:52 UTC ( #11140441=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How do our code evaluations in regex refer to the regex group captured, by name and/or relative way ? Try such
'foo' =~ /(?'name'(?=o)(\w+))(?{print $name})/ # or 'foo' =~ /(?'name'(?=o)(\w+))(?{print $-1})/ Number found where operator expected at (eval 23)
to only get failure.
How is the correct Perl's way ?
  • Comment on Code expressions in regex refer to the regex group captured, by name / relative
  • Download Code

Replies are listed 'Best First'.
Re: Code expressions in regex refer to the regex group captured, by name / relative
by haukex (Archbishop) on Jan 14, 2022 at 01:49 UTC

    See %+ and %-; the following prints "oo":

    use warnings; use strict; 'foo' =~ /(?'name'(?=o)(\w+))(?{print $+{name}})/;

    But also note the documentation of (?{...}):

    WARNING: Using this feature safely requires that you understand its limitations. Code executed that has side effects may not perform identically from version to version due to the effect of future optimisations in the regex engine. For more information on this, see Embedded Code Execution Frequency.
Re: Code expressions in regex refer to the regex group captured, by name / relative
by LanX (Sage) on Jan 14, 2022 at 02:13 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11140441]
Approved by haukex
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2022-08-10 18:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?