SoFunction
Updated on 2025-04-08

Detailed explanation of non-capturing groups of Python regular expressions

In Python regular expressions, non-capturing groups ((?:...)) is a type used forGroupingbutNo matching results are savedstructure. It is with ordinary capture groups(...)The syntax is similar, but no numbered or named groups are created when matching, and are often used to simplify regular expressions and improve performance.

1. Syntax

(?:pattern)
  • ?:expressNon-capturedmark.
  • patternis the regular expression pattern to match.

2. Example comparison

Normal capture group ()

import re
text = "cat dog"
# Capture Groupmatch = (r"(cat) (dog)", text)
print(())  # ('cat', 'dog')
print((1))  # cat
print((2))  # dog

result:groups()Returns all captured subgroups,group(1)group(2)Accessed by number.

Non-capturing group (?:...)

import re
text = "cat dog"
# Non-capture groupmatch = (r"(?:cat) (?:dog)", text)
print(())  # ()
print((0))  # cat dog

result:groups()Returns an empty tuple becauseNo captured group

3. Main application scenarios

1️⃣ Select (|Reduce complexity when )

import re
text = "color colour"
# Capture Groupmatches = (r"(col(or|our))", text)
print(matches)  # [('color', 'or'), ('colour', 'our')]
# Non-capture groupmatches = (r"col(?:or|our)", text)
print(matches)  # ['color', 'colour']

Advantages(?:or|our)It is only used for matching, and does not save subgroups, and the result is simpler.

2️⃣ Improve matching performance

import re
import time
text = "ab" * 100000
# Capture Groupstart = ()
(r"(ab)+", text)
print("Capturing group time-consuming:", () - start)
# Non-capture groupstart = ()
(r"(?:ab)+", text)
print("Non-capture group time-consuming:", () - start)

Advantages: Non-capturing groups do not save matching content, soFasterLess memory usage

3️⃣ Structured complex expressions

import re
text = "2024-02-19"
# Capture Groupmatch = (r"(\d{4})-(\d{2})-(\d{2})", text)
print(())  # ('2024', '02', '19')
# Non-capture groupmatch = (r"(?:\d{4})-(?:\d{2})-(?:\d{2})", text)
print(())  # ()

Advantages: Clear structure, no need to save intermediate matching results.

4. Capture Group vs. Non-capture Group Comparison

characteristic Capture Group(pattern) Non-capture group(?:pattern)
Save the results save,group()Accessible No saving, nogroup()result
Processing performance Slow (need to save the match) Fast (no need to save matches)
Complexity Complex, need to manage group index Simplified for logical grouping
Application scenarios When a match needs to be saved Only matches are not saved

5. Conclusion

  • Subgroup content required: Use a normal capture group(...), can be passedgroup(n)Get it.
  • Control structure only: Use non-capturing groups(?:...), improve performance and simplify expressions.
  • Performance optimization: Non-capturing groups reduce memory consumption and are suitable for large text processing.

This is the article about the introduction of non-capturing groups of Python regular expressions. For more related content of non-capturing groups of Python regular expressions, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!