Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

[RE] match positions of named captures?

by LanX (Archbishop)
on Nov 29, 2019 at 09:04 UTC ( #11109436=perlquestion: print w/replies, xml ) Need Help??

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

In continuation of Re^3: Greedy modifier found to be working non-greedy in a named group
  • There is a way to get the pos of capture groups by index via @- and @+
  • There is a way to get capture groups by name via %+
  • But what's the way to get the positions of a named capture?

I googled and could only find this SO discussion match-positions-of-named-capture-groups-in-perls-regexps but poor Felix didn't get a proper answer.

Though I slightly remember we already had this discussion here...

(something like finding the index of a named capture and accessing @+ / @- probably? Hint: %- doesn't help here)

update

I tried to look into Tie::Hash::NamedCapture , the "Pragmatic Module" used to implement %+ and %- but stopped at the XS barrier

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Replies are listed 'Best First'.
Re: [RE] match positions of named captures?
by Veltro (Friar) on Dec 02, 2019 at 10:23 UTC

    The named captures (or resulting hashes) do not have a relation to the actual position of the place where the group is defined. I think the whole intend of the module has a different purpose and I doubt the developer had group position in mind.

    In the example that follows the results in this particular case foo and bar come from two different groups. How do you expect to get a position for that named group?

    use strict ; use warnings ; use Data::Dumper ; my $str = "abcdef" ; $str =~ /(?<foo>[ab])*(?<bar>c)(?<foo>d)(?<bar>[ef]*)/ ; print Dumper(\%-) ;
    $VAR1 = { 'foo' => [ 'b', 'd' ], 'bar' => [ 'c', 'ef' ] };
      > How do you expect to get a position for that named group?

      by associating a named capture to an capture index. The word tie implies that this is done somewhere.

      $VAR1 = { 'foo' => [ 'b', # $1 'd' # $3 ], 'bar' => [ 'c', # $2 'ef' # $4 ] };

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re: [RE] match positions of named captures?
by haukex (Chancellor) on Dec 02, 2019 at 13:30 UTC
    I tried to look into Tie::Hash::NamedCapture, the "Pragmatic Module" used to implement %+ and %- but stopped at the XS barrier

    I looked at this as well, specifically at FIRSTKEY and NEXTKEY in NamedCapture.xs, because I was hoping that maybe the iteration order would be the same as the order of the capture groups, but sadly that doesn't seem to be the case. Anyway, its implementation appears to use named_buff_iter in regcomp.c, and the central function here appears to be reg_named_buff_nextkey. But that's as far as I've gotten in my research, my understanding of the internals isn't good enough to see if there's an "easy" way to access the capture group name to number mapping.

Re: [RE] match positions of named captures?
by QM (Parson) on Dec 02, 2019 at 07:19 UTC
    I assume you've had a good read of the perlre doc, especially Capture Groups?

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2019-12-05 21:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (151 votes). Check out past polls.

    Notices?