Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: Retrieving Regex matched Group name

by Kenosis (Priest)
on Feb 18, 2013 at 23:11 UTC ( #1019411=note: print w/replies, xml ) Need Help??

in reply to Retrieving Regex matched Group name

If I'm understanding you correctly, it seems that you are attempting to use named capture groups and, if there's a match, retrieve the name of the matching group. If so, consider the following:

use strict; use warnings; my $string = 'word_one word_one word_two 2000 word_three 3000'; while ( $string =~ m/(?<Word>\w+)\s+(?<Digit>\w+)/g ) { my %groupNames; push @{ $groupNames{ $+{$_} } }, $_ for keys %+; print "Match : $1 GroupName(s): @{ $groupNames{$1} }\n"; }


Match : word_one GroupName(s): Word Digit Match : word_two GroupName(s): Word Match : word_three GroupName(s): Word

Group names and their associated values are contained in the hash %+. The above creates a hash of arrays (HoA) keyed on the (captured) values of %+, since two different keys may have identical values. The @{ $groupNames{$1} } notation uses the captured match as a key, and its associated value is the list of capture group names.

Hope this helps!

Replies are listed 'Best First'.
Re^2: Retrieving Regex matched Group name
by Samman_Mahmoud (Initiate) on Feb 19, 2013 at 21:23 UTC
    ThanK U Kenosis that's exactly what I was Looking for :)

      You're most welcome, Samman_Mahmoud!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1019411]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2018-02-19 14:19 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (265 votes). Check out past polls.