Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Retrieving Regex matched Group name

by Samman_Mahmoud (Initiate)
on Feb 18, 2013 at 21:40 UTC ( [id://1019404]=perlquestion: print w/replies, xml ) Need Help??

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

Hii I want to get the matched group name after comparing string to regex expression

while($string=~m/(?<Word>(\w+)?<Digit>(\w+))/g) { print "Match : $1 GroupName: "; }

Replies are listed 'Best First'.
Re: Retrieving Regex matched Group name
by Kenosis (Priest) on Feb 18, 2013 at 23:11 UTC

    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"; }

    Output:

    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!

      ThanK U Kenosis that's exactly what I was Looking for :)

        You're most welcome, Samman_Mahmoud!

Re: Retrieving Regex matched Group name
by CountZero (Bishop) on Feb 18, 2013 at 22:14 UTC
    Please show us some of the strings you are trying to match. It is impossible to check whether the regex is correct if we do not see the input.

    It would also be a helpful to tell what results you expect.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: Retrieving Regex matched Group name
by LanX (Saint) on Feb 18, 2013 at 22:54 UTC
    I suppose your question is how to get the name of a capture-group by index.

    It's not easily possible to associate capture-name to capture-index. We had this recently.

    Consider another concept!

    But if you just want to know how to access the match for a name use %+

    see perlretut: Outside of the pattern a named capture group is accessible through the %+ hash.

    Cheers Rolf

Re: Retrieving Regex matched Group name
by frozenwithjoy (Priest) on Feb 18, 2013 at 22:51 UTC
    Without knowing what you are trying to match, I'm just guessing, but my guess is that you want your regex to be something like this:
    #!/usr/bin/env perl use strict; use warnings; use feature 'say'; my $string = "<Word>Cat<Digit>1 blah blab blah <Word>Dog<Digit>0"; while ( $string =~ m/<Word>(\w+)<Digit>(\d+)/g ) { say "Matched Word: $1"; say "Matched Digit: $2"; } __END__ Matched Word: Cat Matched Digit: 1 Matched Word: Dog Matched Digit: 0

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-16 08:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found