fork download
  1. <?php
  2.  
  3.  
  4.  
  5. $s = '
  6. AAA
  7. //content
  8. :AAA
  9. ';
  10. // this pattern does not match, because of the named subpattern in the first alternative of the condition on recursion?
  11. $p1 = '~(?J)
  12.  
  13. (?(R)
  14. (?P<tag>ZZZ) # with the given subject, recursion shall never occur and thus never use this alternative
  15. |
  16. (?P<tag>AAA) # with the given subject, recursion shall never occur and thus always use this alternative
  17. )
  18.  
  19. [^:]+ # matching //content
  20.  
  21. :(?P=tag) # matching :AAA (last line)
  22.  
  23. ~mx';
  24.  
  25.  
  26. // this pattern matches
  27. $p2 = '~(?J)
  28.  
  29. (?(R)
  30. ZZZ # the only difference with pattern $p1 is this line (no tag used)
  31. |
  32. (?P<tag>AAA)
  33. )
  34.  
  35. [^:]+
  36.  
  37. :(?P=tag)
  38.  
  39. ~mx';
  40.  
  41. // this pattern matches
  42. $p3 = '~(?J)
  43.  
  44. (?(R)
  45. (?P<tag>ZZZ)
  46. |
  47. (?P<tag>AAA)
  48. )
  49.  
  50. [^:]+
  51.  
  52. :\2 # the only difference with pattern $p1 is this line (no tag used)
  53.  
  54. ~mx';
  55.  
  56.  
  57. $p = $p1;
  58. //$p=$p2; // It seems that the named tag in the first alternative causes problem
  59. //$p=$p3; // It seems that the second alternative was captured indeed, but cannot be backreferenced with its name
  60.  
  61. if (preg_match($p, $s, $m)) {
  62. echo 'ok';
  63. }
  64. else{
  65. echo 'oops';
  66. }
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
oops