Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

offset of named capture buffer

by mhgoeschl (Novice)
on Nov 06, 2012 at 13:04 UTC ( #1002470=perlquestion: print w/replies, xml ) Need Help??
mhgoeschl has asked for the wisdom of the Perl Monks concerning the following question:

Hi, using @- and @+ I can get the _offset_ information of any capture porvided I know its ordinal number in the capture list, e.g. $-[2] for (un-named) backref $2. If I work with named backrefs, e.g. (?<myref>), I can easily get its _content_ via $+{myref}. Now, I am seeking to access the _offset_ values of a named backref given its name (here: 'myref') only ? An alternative route would be knowing the ordinal capture number corresponding to a named backref (such that I could access its offset information via the @-/@+ lists). Any idea how to do this ?

Replies are listed 'Best First'.
Re: offset of named capture buffer
by Athanasius (Chancellor) on Nov 06, 2012 at 13:51 UTC

    See also the 2008 thread Finding match offsets of named captures in 5.10 regex, in which it is suggested that a code block can be used to capture the offset with a call to pos.

    Update: An example:

    2:56 >perl -Mstrict -wE "my $s = q[My 25 cents.]; my ($p, $q); $s =~ +/(?<myref>(?{$p = pos})\d+(?{$q = pos}))/; say $+{myref}; say $p, q[ +--> ], $q;" 25 3 --> 5 2:56 >

    Athanasius <°(((><contra mundum

Re: offset of named capture buffer
by Anonymous Monk on Nov 06, 2012 at 13:43 UTC
      esp see
      6524 if (rx && RXp_PAREN_NAMES(rx)) { 6525 HE *he_str = hv_fetch_ent( RXp_PAREN_NAMES(rx), namesv, 0 +, 0 ); 6526 if (he_str) { 6527 IV i; 6528 SV* sv_dat=HeVAL(he_str); 6529 I32 *nums=(I32*)SvPVX(sv_dat); 6530 for ( i=0; i<SvIVX(sv_dat); i++ ) { 6531 if ((I32)(rx->nparens) >= nums[i] 6532 && rx->offs[nums[i]].start != -1 6533 && rx->offs[nums[i]].end != -1) 6534 { 6535 ret = newSVpvs(""); 6536 CALLREG_NUMBUF_FETCH(r,nums[i],ret); 6537 if (!retarray) 6538 return ret;
Re: offset of named capture buffer
by AnomalousMonk (Chancellor) on Jan 30, 2013 at 13:11 UTC

    Per perlre:

        A named capture group. Identical in every respect to normal
        capturing parentheses "()" but for the additional fact ...

    Don't know if this helps much, but if you already know the ordinal number of the capture group associated with a named capture, then  @- @+   work exactly as advertised. (But it sounds as if you want to go from the named group to its capture group ordinal.)

    >perl -wMstrict -le "my $s = 'xxx fooooo yyy'; ;; $s =~ m{ (x+) .*? (?<FOO>fo+) .*? (y+) }xms; ;; print qq{'$1' '$+{FOO}' '$3'}; print qq{'$1' '$2' '$3'}; print qq{/fo+/ at $-[2] to $+[2]}; " 'xxx' 'fooooo' 'yyy' 'xxx' 'fooooo' 'yyy' /fo+/ at 4 to 10

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1002470]
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2018-07-21 04:15 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (444 votes). Check out past polls.