Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: How do I extract named variable names from regex string

by JavaFan (Canon)
on Jan 30, 2012 at 17:09 UTC ( #950814=note: print w/ replies, xml ) Need Help??


in reply to How do I extract named variable names from regex string

Easy, you can let Perl do all the work. Assume your pattern is in $str, then do:

use 5.010; "" =~ /(?:$str)?/; while (my ($key, $matches) = each %-) { if (@$matches > 1) { say "Duplicate name $key"; } }


Comment on Re: How do I extract named variable names from regex string
Download Code
Re^2: How do I extract named variable names from regex string
by LanX (Canon) on Jan 31, 2012 at 00:21 UTC
    Sorry for nitpicking, but in praxis this only works with a string where all groups match, "" isn't enough.

    use 5.010; "" =~ /(?<mon>\w+)\s(?<mon>\w+)/; print scalar keys %-; # 0

    Now constructing such a string is in general even more difficult than just parsing for named captures labels.

    Good idea anyway! :)

    Cheers Rolf

      Thank you for playing, but you fail.

      "" =~ /(?:(?<mon>\w+)\s(?<mon>\w+))?/; say scalar keys %-; # 1
      All that's required is for the pattern to match. Taking the pattern, and wrapping it inside a (?: )? will make it match against "" (except for some degenerate cases). If you look back at my code, this is exactly what I did.
        Ups ... you're right!

        Cheers Rolf

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (14)
As of 2015-07-02 08:29 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 (31 votes), past polls