Обратная ссылка против подпрограммы. Я правильно понял разницу? - PullRequest
0 голосов
/ 08 ноября 2019

С pcre man-страница:

BACK REFERENCES
...
  There  are  several  different ways of writing back references to named
  subpatterns. The .NET syntax \k{name} and the Perl syntax  \k<name>  or
  \k'name'  are supported, as is the Python syntax (?P=name). Perl 5.10's
  unified back reference syntax, in which \g can be used for both numeric
  and  named  references,  is  also supported. We could rewrite the above
  example in any of the following ways:

    (?<p1>(?i)rah)\s+\k<p1>
    (?'p1'(?i)rah)\s+\k{p1}
    (?P<p1>(?i)rah)\s+(?P=p1)
    (?<p1>(?i)rah)\s+\g{p1}

Далее говорится:

ONIGURUMA SUBROUTINE SYNTAX

    For compatibility with Oniguruma, the non-Perl syntax \g followed by  a
    name or a number enclosed either in angle brackets or single quotes, is
    an alternative syntax for referencing a  subpattern  as  a  subroutine,
    possibly  recursively. Here are two of the examples used above, rewrit-
    ten using this syntax:

      (?<pn> \( ( (?>[^()]+) | \g<pn> )* \) )
      (sens|respons)e and \g'1'ibility

    PCRE supports an extension to Oniguruma: if a number is preceded  by  a
    plus or a minus sign it is taken as a relative reference. For example:

      (abc)(?i:\g<-1>)

    Note  that \g{...} (Perl syntax) and \g<...> (Oniguruma syntax) are not
    synonymous. The former is a back reference; the latter is a  subroutine
    call.

Итак, это означает, что обратная ссылка толькоотносится к тому, что было найдено ранее.

В таком случае, почему это работает как обратная ссылка?

Recursive back references

    A back reference that occurs inside the parentheses to which it  refers
    fails  when  the subpattern is first used, so, for example, (a\1) never
    matches.  However, such references can be useful inside  repeated  sub-
    patterns. For example, the pattern

      (a|b\1)+

    matches any number of "a"s and also "aba", "ababbaa" etc. At each iter-
    ation of the subpattern,  the  back  reference  matches  the  character
    string  corresponding  to  the previous iteration. In order for this to
    work, the pattern must be such that the first iteration does  not  need
    to  match the back reference. This can be done using alternation, as in
    the example above, or by a quantifier with a minimum of zero.

Это потому, что для этого не требуется стек?

...