What Capture Groups Do
A capture group is a portion of a regular expression that matches a substring and stores it for later use. You mark a group with parentheses. The regex engine remembers what that part matched, and you can retrieve it by position or by name.
Capture groups are useful when you need to extract specific parts of a matched string. For example, if you are parsing a URL and want the domain separately from the path, you can group the domain with parentheses and refer to it later.
The simplest capture group is just parentheses around a pattern:
(\w+)
This matches one or more word characters and captures them. If the input is "hello world", the group captures "hello".
You can have multiple capture groups in a single pattern. They are numbered from left to right based on the opening parenthesis. The first group is number 1, the second is number 2, and so on.
Named capture groups use a different syntax. They let you refer to a group by name instead of by number, which is helpful when your pattern has many groups and their order matters less than their labels.
(?P<name>pattern)
The exact syntax for named groups varies across implementations. Some use (?P<name>pattern), others use (?<name>pattern). Check your system if you are unsure.
What Lookarounds Do
A lookahead is an assertion that checks whether a pattern follows the current position without consuming any characters. A lookbehind does the same thing but checks what precedes the current position.
Lookarounds do not capture anything. They only validate that a pattern exists at a certain position. This makes them useful for conditional matching where you need to know what comes next but do not want to include it in the result.
A positive lookahead uses the syntax:
pattern(?=follow)
This matches pattern only when it is followed by follow. The follow part is not consumed by the match.
A negative lookahead uses:
pattern(?!follow)
This matches pattern only when it is NOT followed by follow.
Lookbehinds work the same way but check what comes before:
(?<=precede)pattern
(?<!precede)pattern
Lookbehinds are more restrictive than lookaheads. Many implementations require lookbehinds to have a fixed width, meaning the pattern inside must match a known number of characters. Variable-width lookbehinds are supported in some systems but not all.
Combining Capture Groups and Lookarounds
Capture groups and lookarounds work together naturally. You can use a lookahead to constrain where a capture group matches:
(\w+)(?=\s)
This captures word characters only when they are followed by whitespace. The captured text does not include the whitespace.
You can also nest lookarounds inside capture groups:
(?=<div>)(.*?)(?=</div>)
This captures the content between <div> and </div> tags, excluding the tags themselves.
A common pattern is to use lookarounds to avoid capturing unwanted characters. For example, when extracting quoted strings:
(?<=")(.*?)(?=")
This captures the text inside double quotes without including the quotes in the result.
Common Mistakes
One frequent mistake is confusing lookarounds with capture groups. Lookarounds do not consume characters, while capture groups do. This affects what the overall match includes.
Another mistake is assuming that lookbehinds always support variable-width patterns. Many implementations require fixed-width lookbehinds, so a pattern like (?<=\w+)pattern may fail in some systems.
Capture group numbering can also be confusing when groups are nested. The numbering is based on the order of opening parentheses, not the order of closing parentheses.
Testing Patterns
When working with capture groups and lookarounds, testing your patterns is essential. A live testing tool lets you see matches in real time and inspect the captured groups.
RegexBuilder provides a way to test regular expressions with live match highlighting and a plain-English explainer of the pattern. It also includes a pattern library for common patterns and supports multiple regex flavors.
When to Use Each
Use capture groups when you need to extract and reuse parts of a match. Use lookarounds when you need to validate context without including it in the result.
Capture groups are essential for replacement operations where you need to refer back to captured parts. Lookarounds are essential for conditional matching where the context matters but the content does not.
Both are fundamental tools for working with regular expressions, and understanding their differences helps you write patterns that are both correct and efficient.
Tool mentioned: RegexBuilder