<?php
$s = '
AAA
//content
:AAA
';
// this pattern does not match, because of the named subpattern in the first alternative of the condition on recursion?
$p1 = '~(?J)
(?(R)
(?P<tag>ZZZ) # with the given subject, recursion shall never occur and thus never use this alternative
|
(?P<tag>AAA) # with the given subject, recursion shall never occur and thus always use this alternative
)
[^:]+ # matching //content
:(?P=tag) # matching :AAA (last line)
~mx';
// this pattern matches
$p2 = '~(?J)
(?(R)
ZZZ # the only difference with pattern $p1 is this line (no tag used)
|
(?P<tag>AAA)
)
[^:]+
:(?P=tag)
~mx';
// this pattern matches
$p3 = '~(?J)
(?(R)
(?P<tag>ZZZ)
|
(?P<tag>AAA)
)
[^:]+
:\2 # the only difference with pattern $p1 is this line (no tag used)
~mx';
$p = $p1;
//$p=$p2; // It seems that the named tag in the first alternative causes problem
//$p=$p3; // It seems that the second alternative was captured indeed, but cannot be backreferenced with its name
echo 'ok';
}
else{
echo 'oops';
}
PD9waHAgCgoKCiRzID0gJwpBQUEKLy9jb250ZW50CjpBQUEKJzsKLy8gdGhpcyBwYXR0ZXJuIGRvZXMgbm90IG1hdGNoLCBiZWNhdXNlIG9mIHRoZSBuYW1lZCBzdWJwYXR0ZXJuIGluIHRoZSBmaXJzdCBhbHRlcm5hdGl2ZSBvZiB0aGUgY29uZGl0aW9uIG9uIHJlY3Vyc2lvbj8KJHAxID0gJ34oP0opCgooPyhSKQogICAgKD9QPHRhZz5aWlopICAgICAgICAjIHdpdGggdGhlIGdpdmVuIHN1YmplY3QsIHJlY3Vyc2lvbiBzaGFsbCBuZXZlciBvY2N1ciBhbmQgdGh1cyBuZXZlciB1c2UgdGhpcyBhbHRlcm5hdGl2ZQp8CiAgICAoP1A8dGFnPkFBQSkgICAgICAgICMgd2l0aCB0aGUgZ2l2ZW4gc3ViamVjdCwgcmVjdXJzaW9uIHNoYWxsIG5ldmVyIG9jY3VyIGFuZCB0aHVzIGFsd2F5cyB1c2UgdGhpcyBhbHRlcm5hdGl2ZQopCgpbXjpdKyAgICAgICAgICAgIC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