in reply to how to go from numbered captures to named?

(\q<R>): Is that a typo in your post, or are you using \q<R> when you mean \g<R>? ...or do I just not know about the \q<NAME> backreferencing metacharacter? (I'm never confident that I know all of the possible metacharacters for regular expressions. ;)

Isn't it a problem that you're backreferencing a capture group while still inside of it? I don't know for sure, but it seems funky. When I try a minimal example:

(?<name>a.*?\g<name>)

Perl fails to compile the regex.

I also get a failure to compile when I try your last example (after fixing the \q<name> vs \g<name> issue). I really think that trying to access a backreference while in the capture group that created it is asking for trouble.


Dave

Replies are listed 'Best First'.
Re^2: how to go from numbered captures to named?
by perl-diddler (Chaplain) on Jul 15, 2012 at 23:01 UTC
    But you can do it from numbered capture groups? In fact they talk about referring to an earlier capture group to invoke recursion. As for the \q<name> -- that was a typo... It's supposed to be \k<name> (or \k'name'), also for the sake of python/pcre, (?P=name) is also accepted. ARG...I see the prob: It is an error to refer to a name not defined by a "(?<NAME>)" earlier in the pattern. the (2) refer's to the 2nd capture group from within the capture group -- for the name that's not ok, the name has to already be defined (hit the end paren)... Dang!