What is a non-capturing group in regular expressions?
A non-capturing group in regular expressions is a way to group multiple tokens together without storing (or “capturing”) the matched substring in a numbered group. Instead of using parentheses like ( ... )
, you prefix them with ?:
, as in (?: ... )
.
Why Non-Capturing?
- Avoid Unnecessary Captures: Capturing groups
( ... )
store matches that you may reference later (e.g., with backreferences or when retrieving match groups in code). If you don’t need to reference a group’s content, using a capturing group can create unnecessary complexity and overhead. - Improve Readability & Performance: Decluttering extraneous capturing groups can make your regex easier to maintain and sometimes improves performance, especially in large or complex patterns.
Syntax
(?:pattern)
?:
indicates a non-capturing group.pattern
can include multiple tokens, quantifiers, or sub-patterns.
Example
^(?:http|https)://(?:www\.)?[a-z0-9.-]+\.[a-z]{2,}$
^(?:http|https)://
: Matches eitherhttp://
orhttps://
but does not create a capturing group for the protocol.(?:www\.)?
: Optionally matcheswww.
without storing it in a capture group.[a-z0-9.-]+\.[a-z]{2,}$
: Matches the domain and top-level domain (TLD).
Because none of these sub-patterns need to be captured (i.e., we don’t need them in backreferences or as separate matched groups in our code), non-capturing groups keep the regex simpler.
When to Use Non-Capturing Groups
- Grouping for Alternation or Quantifiers: If you only need parentheses for
|
(OR) logic or repeated sub-expressions like(?:abc)+
, a non-capturing group is ideal. - Readability & Maintainability: If you have multiple sections of a regex that you don’t plan to reference, non-capturing groups reduce the clutter of unwanted captured groups.
- Performance: Some regex engines skip extra overhead for non-capturing groups. Although for most typical patterns the difference is minor, it can be beneficial in large or complex regexes.
Capturing vs. Non-Capturing: Quick Comparison
Feature | Capturing Group ( ... ) | Non-Capturing Group (?: ... ) |
---|---|---|
Stores matched text in a group | Yes, accessible via backreferences or code | No, no direct access to the matched text |
Use in alternation/quantifiers | Yes | Yes (preferred if no references are needed) |
Impact on performance | Potential overhead for large patterns | Often lighter, especially in some engines |
Recommended Resources
Bottom line: Use capturing groups if you’ll reference or extract the captured text. Otherwise, non-capturing groups make your regex more concise, clear, and sometimes faster.
CONTRIBUTOR
TechGrind