Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Capturing brackets within a repeat group

by Arien (Pilgrim)
on Jan 11, 2003 at 03:39 UTC ( [id://226043]=note: print w/replies, xml ) Need Help??


in reply to Capturing brackets within a repeat group

The passage from perlre is:

The numbered variables ($1, $2, $3, etc.) and the related punctuation set ($+, $&, $`, $', and $^N) are all dynamically scoped until the end of the enclosing block or until the next successful match, whichever comes first.

So you can only get at the last captured repeated group from outside the regex this way: the earlier captures get overwritten when repeatedly matching the repeated sub-pattern to get an overall match.

&mdash Arien

  • Comment on Re: Capturing brackets within a repeat group

Replies are listed 'Best First'.
Re: Re: Capturing brackets within a repeat group
by BrowserUk (Patriarch) on Jan 11, 2003 at 03:51 UTC

    That's interesting. I've read that passage many times but never interpreted it that way. The bracketed comment you ommitted from the end of the paragraph:

    The numbered variables ($1, $2, $3, etc.) and the related punctuation set ($+, $&, $`, and $') are all dynamically scoped until the end of the enclosing block or until the next successful match, whichever comes first. (See Compound Statements in the perlsyn manpage.)

    led me to think that this was only relevent to the scope of the capture buffers external to the statement itself. I read the phrase you highlighted ... or until the next successful match ... to mean a successful match as part of a distinctly seperate m// or s/// (hence the reference to "Compound Statements"), rather than as the next successful match within the same statement.

    Oh well. It was a nice idea. Thanks for setting me straight.


    Examine what is said, not who speaks.

    The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.

      That's how I read that passage, too.

      But I have no problem with the way repeating a capture works. The ()'s generate code to refer to a particular slot, since the numbering is static from left-to-right. The repeat redoes the same parens. It's no different from backtracking in that respect.

      I'm sure you've figured this out:

      /( (?: blah blah) {5} ) /x
      Put the repeat inside the parens.

      —John

        The thing with doing that is that it captures the whole of the repeat group as a single entity, whereas I was hoping to capture each of the individual parts seperately.


        Examine what is said, not who speaks.

        The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2025-01-22 10:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which URL do you most often use to access this site?












    Results (63 votes). Check out past polls.