0
(?:  [^[]   |   \[  (?! /?indent]   )   |   (?R)    )

(?: ...) - this is a non-capturing group, the whole thing here

(?! ...) - negative lookahead. Specifies a group that cannot match after the main expression (if it matches the result is discarded)

(?R) - recursion

I am at the beginning of learning the regex and I am not understanding how this works as a whole. I guess I am not understanding some aspects like the OR operator or the recursion part?

a) How does the recursion work in the context of (?: A | B | (?R) )

b) Is OR, in general, behaving like this "if A matches I dont go to B?", and again the (?R) part in this context.

Thanks!

  • \[indent]((?:[^[]|\[(?!/?indent])|(?R))+)\[/indent] - this is the whole thing. This is the Example #3 from http://php.net/manual/en/function.preg-replace-callback.php –  Aug 18 '17 at 16:53
  • See [Recursion](http://www.regular-expressions.info/recurse.html), [alternation](http://www.regular-expressions.info/alternation.html). – Wiktor Stribiżew Aug 18 '17 at 16:55
  • Still here? :) I am still not getting that. It has not been answered so maybe it could be unblocked? I am not understanding the "mechanism" of this. "A | B | (?R)" –  Aug 18 '17 at 16:55
  • It is basic alternation. Once a branch matches, parsing of the group is stopped. Read the two links I shared. See also [this](https://stackoverflow.com/questions/10248776/why-order-matters-in-this-regex-with-alternation) and [this](https://stackoverflow.com/questions/18017661/why-does-the-order-of-alternatives-matter-in-regex) thread. – Wiktor Stribiżew Aug 18 '17 at 16:57
  • Ok, this is probably the main thing that I did not understand. So A matches - the whole thing is stopped. A does not match goes to B. B matches, then the (?R) copies something and checks again? If A matches, then the B is skipped but the (?R) is used for "copying" the A? Something along these lines? Like I said, the mechanism of that. –  Aug 18 '17 at 16:58
  • Read the threads I linked to. – Wiktor Stribiżew Aug 18 '17 at 16:59
  • And I am still seeing the (?R) as part of the OR OR statement (it is this). So if A skipped, B skipped, it goes to the (?R) and this is accessing the A or B? Like I said, this is not an obvious thing. This is actually advanced regex, so the example is not that bad. –  Aug 18 '17 at 17:00
  • It has all been explained. Read the existing explanations. – Wiktor Stribiżew Aug 18 '17 at 17:02
  • Ok, this is good too. I also have some urls from other sources so I will read it tomorrow I guess. I am stuck on this since yesterday. I am working on some other aspects and I wanted to get out of that and do the regex next. Now it is like fixing a car first and learning how to fix a car after that, but this is ok. –  Aug 18 '17 at 17:03
  • To understand *`(?R)` copies something and checks again*, you must read about recursion. I already told you in the previous question that `(?R)` recurses the whole subpattern. Just literally: all the pattern. Not just a part. Alternation is very well explained at the regular-expressions.info, and I linked to a good question, too. – Wiktor Stribiżew Aug 18 '17 at 17:07
  • Ok, so basically (?R) pastes the whole "\\[indent] ( (?: [^[] | \[ (?! /?indent]) | (?R)) +) \\[/indent]" in its own place if the regex engine gets to that point (the "(?R)"? –  Aug 19 '17 at 06:48
  • Yes, that is what recursion is about. `(?R)` pastes the whole pattern, up to a certain limit that is set in the [PCRE settings](http://php.net/manual/en/pcre.configuration.php), `pcre.recursion_limit`. – Wiktor Stribiżew Aug 19 '17 at 07:45

0 Answers0