Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

When should I use a capturing group or a non-capturing group in a regex?

Quick answer

Use a capturing group, (…), when you need its matched text as a result or to refer to it later. Use a non-capturing group, (?:…), when parentheses are only grouping pattern parts. For example, capture the word and number in item-42 but use a non-capturing group for the hyphen if you do not need it in the results. Captures are numbered in opening-parenthesis order, so adding one can shift later group numbers.

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I’m matching a word, a hyphen, and a number. How do I decide which parentheses should capture?

Geez

Ask which pieces you need after the match. If you need the word and number but not the hyphen, capture the word and number, and use a non-capturing group for the hyphen. In Python’s standard re module and JavaScript, (…) captures a submatch; (?:…) groups pattern parts without adding a captured value. [S1][S2]

For example, with item-42:

(\w+)(?:-)(\d+)

This pattern has two captured groups: the word and the digits. The hyphen is required by the pattern but is not a captured group. If you want the hyphen as a separate captured result, use:

(\w+)(-)(\d+)

Choose based on the text you want to retrieve, not just on how the parentheses look. A non-capturing group is useful for organizing a pattern without adding another capture.

Follow-up question

What exactly changes in the results between those two versions?

Geez

Assuming the input is item-42, the first pattern has two captured groups: item and 42. The second has three: item, -, and 42. [S1][S2]

Here is the group structure side by side:

(\w+)(?:-)(\d+)   → group 1: item; group 2: 42
(\w+)(-)(\d+)     → group 1: item; group 2: -; group 3: 42

In Python’s standard re module, the captured text can be retrieved from the match. In JavaScript, captured values appear in the match-result array in the order of the capturing groups. The non-capturing hyphen does not add a captured value in either case. [S1][S2] These examples describe the captures for this input; the exact way you obtain a match depends on how you use the language’s API.

Follow-up question

Why can changing a group from non-capturing to capturing break code that uses group numbers?

Geez

Capturing groups are numbered by the order of their opening parentheses. In (\w+)(?:-)(\d+), the digits are group 2. If you change the hyphen group to capturing, as in (\w+)(-)(\d+), the hyphen becomes group 2 and the digits become group 3. [S1][S2]

Code that expects the digits at group number 2 may then retrieve a different capture. JavaScript’s match-result array also places captures in capture order, so the digits move to the next position there as well. Before changing a pattern, review every numeric group reference and every place your code reads a numbered capture.

Follow-up question

Can you give me a copyable pair and a quick way to check which version I want?

Geez

Compare these patterns for the sample input item-42:

(\w+)(-)(\d+)     → captures: item | - | 42
(\w+)(?:-)(\d+)   → captures: item | 42

Use the first when you want the hyphen as a captured result; use the second when you need the hyphen in the pattern but not among the captures. In JavaScript, the captured values are listed in the match-result array in capture order. In Python’s standard re module, captured text can be retrieved from the match. [S1][S2]

A quick check is to list the values you want your code to retrieve, then count the capturing parentheses from left to right. Do not count (?:…) as a capture. After editing a pattern, recheck any code that refers to captures by number. These syntax and numbering notes apply to Python’s standard re module and JavaScript regular expressions, not every regex engine or API. [S1][S2]

A practical checklist

  • List the exact substrings your code needs after matching.

  • Use (…) for text you want to capture and (?:…) for grouping alone.

  • Number capturing groups in opening-parenthesis order; non-capturing groups add no capture.

  • After changing a pattern, recheck numeric references and result positions.

  • Try a representative input and inspect the captured values.

A few more useful answers

Does a non-capturing group still affect whether the pattern matches?

Yes. It groups parts of the pattern without adding a captured value. In the example, (?:-) still requires the hyphen.

If I add a capturing group, do later group numbers stay the same?

Not necessarily. Adding a capture can shift the numbers of later captures, so review code that refers to them by number.

Do these examples apply to every regex engine?

No. The syntax and behavior described here are for Python’s standard re module and JavaScript regular expressions. Check the documentation for another engine or API.

Sources

References used for this guide.

  1. [S1] Python 3.15 re — Regular expression operations
    docs.python.org · Checked
  2. [S2] MDN JavaScript Guide: Groups and backreferences
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work