Sub topic 9
Capture Groups: Returning Several Parts at Once
This chapter assumes you can write a basic REGEXEXTRACT formula and know the RE2 pieces from the previous chapters: character classes such as \d and [A-Z], quantifiers such as + and {4}, and anchors such as ^ and $. Here you add one feature: parentheses that split a single match into several returned values.
What a capture group is
A capture group is a part of a pattern wrapped in parentheses. The regex engine matches the whole pattern as usual. It also remembers the text matched by each pair of parentheses. REGEXEXTRACT then uses those remembered pieces as its result.
Take the text ORD-10492 in cell A2. Compare two formulas:
=REGEXEXTRACT(A2, "[A-Z]+-\d+")
=REGEXEXTRACT(A2, "([A-Z]+)-(\d+)")The first formula has no groups, so it returns the whole match, ORD-10492, in one cell. The second has two groups. It returns ORD in the cell with the formula and 10492 in the cell to its right. The hyphen was matched, but it sits outside the parentheses, so it is not returned.
The rules for what comes back
REGEXEXTRACT follows a small set of rules:
- With no capture groups, it returns the whole first match.
- With one or more capture groups, it returns only the text matched by the groups. The text outside the groups is matched but discarded.
- Groups are returned left to right, in the order of their opening parentheses.
- Each group's text goes into its own cell, so the result is a row that spills to the right.
- Only the first match in the text is used. If the text contains several order IDs, you get the groups from the first one.
- Every returned value is text, even when it looks like a number.
The following diagram shows rules 1 to 4 for one input.
Worked example: splitting structured IDs
Suppose column A holds product codes in the form ORD-10492, INV-2207, and RET-88. You want the prefix and the number in separate columns. Put this in B2:
=REGEXEXTRACT(A2, "^([A-Z]+)-(\d+)$")The formula fills B2 with the prefix and C2 with the number. The ^ and $ anchors make the whole cell match the pattern. Without them, text like see ORD-10492 later would also produce a result, which may be what you want or may hide bad data.
Here is a second example, a date in ISO format, 2026-11-28:
=REGEXEXTRACT(A2, "(\d{4})-(\d{2})-(\d{2})")This returns three cells: 2026, 11, and 28. It is a handy way to get year, month, and day when the cell holds text rather than a real date.
A third example reorders a name. For Lovelace, Ada in A2:
=REGEXEXTRACT(A2, "^([^,]+),\s*(.+)$")Group 1 is everything up to the comma ([^,]+ means one or more characters that are not a comma). The \s* consumes any spaces after the comma without capturing them. Group 2 is the rest of the text. B2 gets Lovelace and C2 gets Ada. To show the first name first, use =C2&" "&B2.
Choosing which group you need
Sometimes you want a single group from a pattern that has several. Wrap the call in INDEX and give it a row number of 1 and a column number for the group:
=INDEX(REGEXEXTRACT(A2, "^([A-Z]+)-(\d+)$"), 1, 2)This returns only 10492. The result of REGEXEXTRACT is a one-row array, so the row argument is always 1. The column argument picks the group.
This is useful when you need a single value for another formula, such as in VALUE(INDEX(...)), and don't want results spilling across cells.
Grouping without capturing
Parentheses do two jobs in a regular expression. They capture text, and they group parts of a pattern so that a quantifier or an alternation applies to the whole part. Sometimes you need the second job without the first, because every capturing group adds a cell to the result.
RE2 supports non-capturing groups with the syntax (?:...). Consider matching ORD, INV, or RET before the number, and capturing only the number:
=REGEXEXTRACT(A2, "(?:ORD|INV|RET)-(\d+)")The group (?:ORD|INV|RET) limits the alternation | to the three prefixes. Because it is non-capturing, the formula returns one cell, the number. If you wrote (ORD|INV|RET)-(\d+) instead, you would get two cells: the prefix and the number.
Use this rule of thumb. Parentheses you want to read from get a plain ( ). Parentheses that only organize the pattern get (?: ).
Optional groups and empty results
A group can be optional. Take prices that sometimes include cents: 12 or 12.50.
=REGEXEXTRACT(A2, "(\d+)(?:\.(\d+))?")The pattern has two capturing groups. The second sits inside an optional non-capturing group, (?:\.(\d+))?. For 12.50 you get 12 and 50. For 12 the second group takes part in no match, and its cell comes back empty. The number of returned cells depends on the pattern, not on the input, so the layout stays stable from row to row.
Keep that in mind when you copy formulas down a column. The result width is fixed by the number of capturing groups, and optional groups only change whether a cell has content.
A repeated group keeps only its last pass
A quantifier on a capturing group does not return a cell per repetition. The group holds the text from its final repetition only. For a1b2c3 and the pattern ([a-z]\d)+, the whole match is a1b2c3, but the group returns just c3.
To capture a repeated pattern in full, put the quantifier inside the group: ((?:[a-z]\d)+) returns a1b2c3. To get every match in the text, REGEXEXTRACT is the wrong tool, because it returns only the first match. Use SPLIT on a delimiter instead, or one REGEXEXTRACT call for each position you need.
Keeping the whole match along with the parts
Because rule 2 drops the ungrouped text, you cannot get the full match and its parts from a single pattern unless you capture the full match too. Wrap the entire pattern in an outer group:
=REGEXEXTRACT(A2, "(([A-Z]+)-(\d+))")Groups are numbered by their opening parenthesis, so the outer group is first. The result is three cells: ORD-10492, ORD, and 10492.
Applying it to a whole column
A multi-group result works with ARRAYFORMULA, which applies the extraction to every row of a range:
=ARRAYFORMULA(REGEXEXTRACT(A2:A10, "^([A-Z]+)-(\d+)$"))Nine input rows produce a block of nine rows and two columns. The formula sits in one cell, and the block spills down and to the right. Everything the block needs must be empty, or you get a #REF! error.
Some rows will not match. A single failing row would show #N/A in both of its cells, so wrap the whole expression in IFERROR:
=ARRAYFORMULA(IFERROR(REGEXEXTRACT(A2:A10, "^([A-Z]+)-(\d+)$"), ""))Rows that fail the pattern now show empty cells, and the others show their groups.
Because captured text is always text, convert numeric groups when you need to calculate with them. You can do this by extracting only the number with a single group, so that VALUE applies to the whole result:
=ARRAYFORMULA(IFERROR(VALUE(REGEXEXTRACT(A2:A10, "-(\d+)$")), ""))Applying VALUE to a result that contains ORD would produce an error, so convert only columns that hold digits.
Behavior summary
| Pattern feature | Effect on the result | Example pattern | Column |
|---|---|---|---|
| No groups | Whole first match, one cell | `\d+` | |
| `( )` capturing group | One cell per group, left to right | `(\d+)-(\d+)` | |
| `(?: )` non-capturing group | No extra cell | `(?:ORD\ | INV)-(\d+)` |
| Optional group that did not take part | Empty cell, same width | `(\d+)(?:\.(\d+))?` | |
| Quantified group `( )+` | Last repetition only | `([a-z]\d)+` | |
| No match at all | `#N/A` error | any pattern the text fails |
Common mistakes with groups
- Forgetting that groups hide the rest of the match. If you add parentheses to a pattern that used to work, the output changes from the whole match to just the grouped part.
- Counting parentheses wrongly. Nested groups are numbered by their opening parenthesis. Write the pattern, count the
(characters that are not followed by?:, and compare with the cells you expect. - Occupied cells to the right. The result needs one empty cell per group. A stray value blocks the spill and gives
#REF!. - Unescaped literal characters. A literal dot, plus sign, or parenthesis in the text must be escaped in the pattern, for example
\.or\(. An unescaped.matches any character. - Quotes inside the pattern. To match a double quote, double it inside the formula string:
"""(\w+)"""extracts a word wrapped in quotes.
Exercise
Column A contains values like Smith, John (Sales). Write a formula for B2 that returns three cells: last name, first name, and department, with no parentheses or comma in the results. Then say what B2:D2 would show for Smith, John (Sales), and what you would change so that department names containing spaces, such as Customer Support, also work.
Show answer
Use =REGEXEXTRACT(A2, "^([^,]+),\s*([^ (]+)\s*\(([^)]+)\)$"). Group 1 ([^,]+) takes everything before the comma. \s* skips the spaces. Group 2 ([^ (]+) takes the first name, stopping at a space or an opening parenthesis. \s*\( skips the space and the literal parenthesis, which is escaped and outside the groups. Group 3 ([^)]+) takes everything up to the closing parenthesis, and \)$ ends the match. For Smith, John (Sales), B2 shows Smith, C2 shows John, and D2 shows Sales. The third group [^)]+ already accepts spaces, so Customer Support works as written. A first name with a space, such as Mary Ann, would need a wider second group, for example ([^(]+?) followed by \s*\(. To apply the formula to the whole column, wrap it as =ARRAYFORMULA(IFERROR(REGEXEXTRACT(A2:A100, "..."), "")) with the same pattern.
Exercise
The text in A2 is Invoice INV-2207 paid. What does =REGEXEXTRACT(A2, "(?:INV|ORD)-(\d+)") return, and what would it return if you removed ?:?
Show answer
With (?:INV|ORD) non-capturing, only (\d+) is a capturing group, so the formula returns one cell: 2207. Without ?:, (INV|ORD) becomes a capturing group, so the formula returns two cells: INV and 2207.