Skip to content

recursion

Bruce Armstrong edited this page Oct 26, 2016 · 1 revision

Recursive Expressions

In addition to backreferences, you can refer to a sub-expression with \gN or ?N where N is a number representing the group. ?0 (or ?R) can be used for the entire pattern.

A palindrome can be checked with:

/
(\w)      # First Letter
(?:       # Need a group, but don't care to capture
 (?0)     # Recurse to see if we are finding a larger palindrome
 |        # Alternation, because there might not be one more
 \w?      # A single letter on its own can be part of this
)         # End the group
\1        # Match the first letter
/x

Exercises

Regex101 | Debuggex [delete]: # (https://regex101.com/delete/93fPaVDSldxvfijDE2ZZGDoC)

  • Write an expression to check for balanced parenthesis

Clone this wiki locally