TExcelTAD
Home › Regular Expressions › Groups & Capturing

Groups & Capturing

Parentheses ( ) do two jobs at once:

  1. Grouping — they turn several items into one unit, so a quantifier or an alternation can apply to all of them.
  2. Capturing — they remember the exact text that the group matched, so you can read it afterwards or reuse it in a replacement.

Grouping for quantifiers

regex · try it yourself
/(\d{1,3}\.){3}\d{1,3}/g
Servers: 10.0.0.1 and 192.168.1.20
2 matches
#1 "10.0.0.1" at index 9
   group 1: "0."
#2 "192.168.1.20" at index 22
   group 1: "1."

(\d{1,3}\.){3} means "one to three digits and a dot — three times". Notice the reported group: when a group is repeated, it keeps only the text from its last repetition (0. and 1.), not all of them.

Note: This pattern checks the shape of an IPv4 address only; it happily accepts 999.999.999.999. Checking ranges with regex is possible but ugly — parse the numbers in code instead.

Capturing parts of a match

Each opening parenthesis creates a numbered group, counted from left to right by the position of the (. Group 0 is always the whole match.

regex · try it yourself
/(\w+)@(\w+)\.(\w+)/
contact: hoa@example.org
1 match
#1 "hoa@example.org" at index 9
   group 1: "hoa"
   group 2: "example"
   group 3: "org"

Nested groups

Groups can nest. Count the opening parentheses to know the numbers:

( ( \d{4} ) - ( \d{2} ) )
1 2         3
regex · try it yourself
/((\d{4})-(\d{2}))-(\d{2})/
Date: 2025-07-31
1 match
#1 "2025-07-31" at index 6
   group 1: "2025-07"
   group 2: "2025"
   group 3: "07"
   group 4: "31"

Groups that didn't take part

If a group sits inside an optional part or an alternative that wasn't used, its value is undefined in JavaScript (None in Python):

regex · try it yourself
/(\d+) (kg|(lb))/g
5 kg and 12 lb
2 matches
#1 "5 kg" at index 0
   group 1: "5"
   group 2: "kg"
   group 3: undefined
#2 "12 lb" at index 9
   group 1: "12"
   group 2: "lb"
   group 3: "lb"

Using groups in code

In JavaScript, match (without g) and exec return an array: index 0 is the whole match and 1, 2, … are the groups. Destructuring makes this neat:

js · try it yourself
const line = "Total: 1,250.75 USD";
const m = line.match(/([\d,]+)\.(\d{2}) ([A-Z]{3})/);
const [, whole, cents, currency] = m;
console.log(whole, cents, currency);
console.log(m.index, m[0]);
1,250 75 USD
7 1,250.75 USD

In replacements, $1, $2, … insert the captured text. Here a date is reordered from year-month-day to day/month/year:

js · try it yourself
const s = "Due 2025-07-31, paid 2025-08-02";
console.log(s.replace(/(\d{4})-(\d{2})-(\d{2})/g, "$3/$2/$1"));
Due 31/07/2025, paid 02/08/2025

Python works the same way, with \1 (or \g<1>) in the replacement:

python · try it yourself
import re

m = re.search(r"(\w+)@(\w+)\.(\w+)", "contact: hoa@example.org")
print(m.group(0), m.group(1), m.groups())
print(re.sub(r"(\d{4})-(\d{2})-(\d{2})", r"\3/\2/\1", "Due 2025-07-31"))
hoa@example.org hoa ('hoa', 'example', 'org')
Due 31/07/2025

Groups at a glance

Syntax Name Captures? Lesson
(...) capturing group yes, by number this one
(?:...) non-capturing group no next
(?<name>...) named group yes, by name and number Named Groups
\1, \k<name> backreference reuses a capture inside the pattern Backreferences
(?=...) (?!...) lookahead no Lookahead
(?<=...) (?<!...) lookbehind no Lookbehind

Test yourself

In /(a(b)(c))/, which group captures b?

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

What does group 1 hold after /(\d)+/ matches 123?

  1. 123
  2. 1
  3. 3
  4. ['1','2','3']

What is a group's value in JavaScript when it did not participate in the match?

  1. ""
  2. null
  3. undefined
  4. 0