Quick Start
Tutorial
Search & Replace
Tools & Languages
Examples
Reference
Recursion & Subroutines
Recursion
Subroutines
Infinite Recursion
Recursion & Quantifiers
Recursion & Capturing
Recursion & Backreferences
Recursion & Backtracking
Regex Tutorial
Introduction
Table of Contents
Special Characters
Non-Printable Characters
Regex Engine Internals
Character Classes
Character Class Subtraction
Character Class Intersection
Shorthand Character Classes
Dot
Anchors
Word Boundaries
Alternation
Optional Items
Repetition
Grouping & Capturing
Backreferences
Backreferences, part 2
Named Groups
Relative Backreferences
Branch Reset Groups
Free-Spacing & Comments
Unicode Characters & Properties
Mode Modifiers
Atomic Grouping
Possessive Quantifiers
Lookahead & Lookbehind
Lookaround, part 2
Lookbehind Limitations
(Non-)Atomic Lookaround
Keep Text out of The Match
Conditionals
Balancing Groups
Recursion and Subroutines
POSIX Bracket Expressions
Zero-Length Matches
Continuing Matches
Backtracking Control Verbs
Control Verb Arguments
More on This Site
Introduction
Regular Expressions Quick Start
Regular Expressions Tutorial
Replacement Strings Tutorial
Applications and Languages
Regular Expressions Examples
Regular Expressions Reference
Replacement Strings Reference
Book Reviews
Printable PDF
About This Site
RSS Feed & Blog
RegexBuddy—Better than a regular expression tutorial!

Regular Expression Recursion and Subroutines

Recursion

Recursion matches the whole regex again at a particular point inside the regex, which makes it possible to match balanced constructs.

Subroutine Calls

Subroutine calls allow you to write regular expressions that match the same constructs in multiple places without having to duplicate parts of your regular expression.

Infinite and Endless Recursion

Infinite recursion occurs if there is a path through the regex that allows it to recurse without having matched anything. Endless recursion occurs if every path through the regex requires it to recurse. Regex engines handle these situations differently (or not at all which causes a crash upon infinite recursion).

Quantifiers on Recursion

Quantifiers can be used on recursion but may not work the way you might expect.

Capturing Recursion and Subroutines

Capturing groups inside recursion and subroutine calls are handled differently by the regex flavors that support them.

Backreferences with Recursion Level

Special backreferences match the text stored by a capturing group at a particular recursion level, instead of the text most recently matched by that capturing group.

Backtracking after Recursion and Subroutines

The regex flavors that support recursion and subroutine calls backtrack differently after a recursion or subroutine call fails.