| Recursion & Subroutines |
| Recursion |
| Subroutines |
| Infinite Recursion |
| Recursion & Quantifiers |
| Recursion & Capturing |
| Recursion & Backreferences |
| Recursion & Backtracking |
Recursion matches the whole regex again at a particular point inside the regex, which makes it possible to match balanced constructs.
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 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.
| Quick Start | Tutorial | Search & Replace | Tools & Languages | Examples | Reference |
| Recursion | Subroutines | Infinite Recursion | Recursion & Quantifiers | Recursion & Capturing | Recursion & Backreferences | Recursion & Backtracking |
| 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 |
Page URL: https://www.regular-expressions.info/recursecnt.html
Page last updated: 16 June 2025
Site last updated: 29 October 2025
Copyright © 2003-2025 Jan Goyvaerts. All rights reserved.