REGEXEXTRACT

Short answer

Pull invoice numbers, dates, or IDs from messy text

REGEXEXTRACT(text, pattern, [return_mode], [case_sensitivity])
Excel version
365
Also searched as
extract po number from text, extract invoice number, regex extract excel, pull id from memo field, extract number from text

What it returns

REGEXEXTRACT searches a string for a regular expression and returns the text that matches. By default it returns the first match. With return_mode set, it returns every match, or the capture groups of the first one, as a spilled array.

=REGEXEXTRACT("AP accrual PO-48120 Northwind", "PO-\d+") returns PO-48120. The PO number can sit at the start of the memo, the end, or between two other references. The pattern finds it by its shape.

If nothing matches, REGEXEXTRACT returns #N/A. That is useful: a memo with no PO number shows up as an error you can count, not as a blank.

Everything it returns is text. 48120 extracted from a memo is the text 48120, not the number, which matters the moment you look it up or sum it.

REGEXEXTRACT is a Microsoft 365 function. It is not in Excel 2024 or any perpetual version.

Arguments that matter

ArgumentRequiredWhat it does
textYesThe string to search. A cell, or a column such as GL[Memo].
patternYesThe regular expression describing what to pull out.
return_modeNo0 first match (default). 1 all matches, as an array. 2 the capture groups of the first match, as an array.
case_sensitivityNo0 case-sensitive (default). 1 case-insensitive.

return_mode of 2 is the one worth learning. Parentheses in a pattern mark capture groups, and mode 2 returns just what each group caught. "PO-(\d+)" with mode 2 returns 48120 without the PO- prefix. "(\d{2})/(\d{2})/(\d{4})" returns day, month and year as three cells.

return_mode of 1 returns every match in the string. A memo referencing two invoices gives you both, which is good for review and dangerous as an input. Two results where you expected one means the row needs a human.

In a financial model

Memo and description fields carry the references the model needs and the ERP never put in their own column. PO numbers for a three-way match. Contract IDs for revenue schedules. Invoice numbers for an AP aging tie-out. Project codes for capex tracking.

None of them sit at a fixed position, so LEFT, MID and FIND cannot reach them reliably. A pattern can, because a PO number always looks like a PO number. When the value does sit between fixed delimiters, as the segments of a GL string do, TEXTSPLIT is the simpler tool.

Keep the extract as delivered, per raw data stays raw, and add the extracted reference as a new column beside it. Then XLOOKUP matches that reference against the PO register or the contract list.

Count the #N/A rows in the check row, as build a check row and make it loud argues. Rows without a reference are often exactly the rows audit asks about.

Worked example

A GL extract formatted as a Table named GL, with a Memo column. Memos are free text typed by different AP clerks:

MemoPO
Freight Jan PO-48120 Northwind48120
po 48377 / Contoso leasing48377
Inv 99102 ref PO4850148501
Accrual reversal DecNO PO

The PO always has five digits, sometimes with PO-, sometimes PO, sometimes po and a space. As a calculated column PO:

=IFNA(--REGEXEXTRACT([@Memo], "PO[- ]?(\d{5})", 2, 1), "NO PO")

The pattern allows an optional hyphen or space after PO. Mode 2 returns only the five digits inside the parentheses. Case-insensitive mode 1 catches po. The double negative turns the text into a number so it matches the PO register. IFNA handles only the no-match case.

The third row also holds an invoice number, 99102, five digits like the PO. The pattern skips it because it requires the PO prefix. In the check row:

=COUNTIF(GL[PO], "NO PO")

Old way vs new way

The old way, which only works if the reference always uses PO-:

=IFERROR(MID([@Memo], FIND("PO-",[@Memo])+3, 5), "NO PO")

The new way:

=IFNA(--REGEXEXTRACT([@Memo], "PO[- ]?(\d{5})", 2, 1), "NO PO")

The FIND version breaks on the second and third rows above and returns nothing useful. The pattern describes every variant the clerks type, and IFNA stops it hiding other errors the way IFERROR would.

Where it breaks

REGEXEXTRACT returns #N/A on rows that have a reference. The text differs from the pattern in a way you did not plan for: lowercase, a space instead of a hyphen, six digits instead of five. Fix: list the failing rows with FILTER, read them, and widen the pattern. Set case_sensitivity to 1 if case does not matter.

The extracted number matches nothing in the register. The result is text. Fix: convert with -- or VALUE before the lookup, or store register keys as text.

The wrong number comes back. The pattern \d{5} matched the invoice number because it appears before the PO in the memo. Fix: anchor on the label, PO[- ]?(\d{5}), and return the group with mode 2.

#SPILL! in a Table column. return_mode 1 or 2 returns an array, and Tables do not allow spills. Mode 2 with one capture group returns one cell and is fine. Fix: use a single capture group, or wrap with INDEX to take one item, or move the formula outside the Table.

IFERROR hides a broken formula. Wrapping in IFERROR also swallows the error a malformed pattern returns, and every row reads "NO PO". Fix: use IFNA, which only catches the no-match case.

#NAME? on another machine. REGEXEXTRACT is Microsoft 365 only. Fix: if the file has to open in Excel 2024 or earlier, extract in Power Query at load.

Related functions

  • REGEXTEST. Same pattern language, returns TRUE or FALSE. Use it to flag rows before you extract from them.
  • REGEXREPLACE. Removes or rewrites the matches instead of returning them.
  • TEXTBEFORE and TEXTAFTER. Simpler and faster when the value sits next to a fixed delimiter.
  • XLOOKUP. Matches the extracted reference against the PO register or contract list.
  • COUNTIFS. Counts rows with no reference, split by vendor or entity, for the check row.

All functions