By only showing the current level, you do not create a spaghetti of patterns and sub-patterns, as the paste method tends to do.
For instance, at step S4, we reach depth D1, and the expression shown is the pattern from the depth level being evaluated. ✽ In the "Position in Regex" column, you have the expression of the current depth level. The other levels are all made optional by the question mark at the end of (?R)?, so they are allowed to fail, as happens at Step S7, Depth D2.
In this example you can see how we start at D0, then go to D1 and D2, then back up to D1, and finally D0, which the expression needs to complete in order to succeed. For easier reference, I call these depths D0, D1, D2 etc. ✽ In the next column, you will have the depth level of the recursion. For easier reference later, I call these steps S1, S2, S3 etc. ✽ In the leftmost column of your spreadsheet, enter the step number. Basically, (?R) means "paste the entire regular expression right here, replacing the original (?R).įirst, it matches three word characters: \w(?R)? # aaa111bbb222 # D0 succeeds. In PCRE and Perl, you just add (?R) anywhere you like in your pattern. When Perl introduced recursion into its regex it behaved differently from PCRE's, and PCRE stuck to its original behavior for backward compatibility. This explains the differences in behavior between the two engines-which, if you insist on knowing at this early stage, pertain to atomicity and the carryover of capture buffers down the stack. ✽ JGSoft (not available in a programming language)Ī bit of history: recursion appeared in PCRE seven years before it made it to Perl regex. Recursion is supported in the following engines: If at that stage you are still interested, you can return here to dive deeper.įor easy navigation, here are some jumping points to various sections of the page: Before you invest your time sutdying this topic, I suggest you start out with the recursion summary on the main syntax page. Recursion is an arcane but immensely helpful feature that only a few regex engines support.