TExcelTAD
Home › Regular Expressions › Non-capturing Groups

Non-capturing Groups

Sometimes you need parentheses only for grouping — to apply a quantifier or limit an alternation — and you don't care about the captured text. Write (?:...) for a non-capturing group. It groups exactly like (...) but doesn't create a numbered group.

Why bother?

Compare two ways to match a price with an optional currency word:

regex · try it yourself
/(\d+)(\.\d\d)? (USD|EUR|VND)/g
12.50 USD, 300 EUR
2 matches
#1 "12.50 USD" at index 0
   group 1: "12"
   group 2: ".50"
   group 3: "USD"
#2 "300 EUR" at index 11
   group 1: "300"
   group 2: undefined
   group 3: "EUR"

If all you want is the amount and the currency, group 2 is noise — and worse, it shifts the currency to $3. Make the decimal part non-capturing:

regex · try it yourself
/(\d+(?:\.\d\d)?) (USD|EUR|VND)/g
12.50 USD, 300 EUR
2 matches
#1 "12.50 USD" at index 0
   group 1: "12.50"
   group 2: "USD"
#2 "300 EUR" at index 11
   group 1: "300"
   group 2: "EUR"

Now group 1 is the full amount and group 2 is the currency. The benefits:

Benefit Why
Stable numbering Adding a grouping later doesn't renumber $1, $2, …
Cleaner results match, findall and split return only the parts you want
Slightly faster The engine doesn't have to record the group's text
Clear intent A reader sees "just grouping" at a glance

Common uses

Applying a quantifier to a sequence:

regex · try it yourself
/\b(?:\d{3}-){2}\d{4}\b/g
555-123-4567 and 55-123-4567
1 match
#1 "555-123-4567" at index 0

Limiting an alternation:

regex · try it yourself
/\b(?:https?|ftp):\/\/\S+/g
see https://a.example, http://b.example, ftp://files.example, mailto:x
3 matches
#1 "https://a.example," at index 4
#2 "http://b.example," at index 23
#3 "ftp://files.example," at index 41

Notice the trailing commas: \S+ happily includes punctuation. A stricter version could end with [^\s,]+ instead — always look at the edges of your matches.

Where it really matters: split and findall

In JavaScript, split inserts every captured group into the result array. Usually that's unwanted:

js · try it yourself
const s = "a1b22c";
console.log(s.split(/(\d)+/));
console.log(s.split(/(?:\d)+/));
console.log(s.split(/\d+/));
[ 'a', '1', 'b', '2', 'c' ]
[ 'a', 'b', 'c' ]
[ 'a', 'b', 'c' ]

Python's re.findall returns the groups instead of the whole match as soon as the pattern has any capturing group. A non-capturing group keeps the whole match:

python · try it yourself
import re

text = "colour color colouur"
print(re.findall(r"colo(u)?r", text))
print(re.findall(r"colo(?:u)?r", text))
['u', '']
['colour', 'color']

Other (?...) constructs

(?: is the first of several "extension" groups that start with (?. None of them capture, except named groups:

Syntax Meaning
(?:...) group, no capture
(?<name>...) named capturing group
(?=...) / (?!...) positive / negative lookahead
(?<=...) / (?<!...) positive / negative lookbehind
(?i:...) inline modifiers for just this group (Python, Java, .NET, PCRE; newest JavaScript engines)

Tip: A good default: use (?:...) whenever you group, and switch to a capturing or named group only for the parts you actually need to read back.

Test yourself

How many capturing groups does /(?:ab)+(c)/ have?

  1. 0
  2. 1
  3. 2
  4. 3

What does "x-y".split(/(-)/) return in JavaScript?

  1. ['x', 'y']
  2. ['x', '-', 'y']
  3. ['x-y']
  4. ['-']

Why might you replace (\.\d+)? with (?:\.\d+)??

  1. To make it optional
  2. So it no longer shifts later group numbers
  3. To make it lazy
  4. To match a literal ?