How to extract text between two words in Google Sheets
Google Sheets formula
=IFERROR(REGEXEXTRACT(A2,$E$1&" (.+?) "&$G$1),"")Capture what sits between the words with a lazy group: =REGEXEXTRACT(A2,"for (.+?) shipped"). Build the pattern from cells to change the words easily.
- Result on the example
- one result per row in
B2, filled down toB6 - Excel
- See below
See it work
The formula is already in B2, filled down to B6. Change any input and the result updates, or edit the formula to experiment.
How it works
The pattern becomes for (.+?) shipped. The brackets capture what REGEXEXTRACT returns, and .+? means "as few characters as possible", so the match stops at the first "shipped" after "for". IFERROR returns a blank for "Order 4475 cancelled", which has neither word.
=IFERROR(REGEXEXTRACT(A2,$E$1&" (.+?) "&$G$1),"")Result"Contoso"
A2"Order 4471 for Contoso shipped today"The value in A2.
$E$1"for"The value in E1.
$E$1&" (.+?) ""for (.+?) "The & operator joins as text.
$G$1"shipped"The value in G1.
$E$1&" (.+?) "&$G$1"for (.+?) shipped"The & operator joins as text.
REGEXEXTRACT(A2,$E$1&" (.+?) "&$G$1)"Contoso"REGEXEXTRACT: The first match, or the captured group(s). #N/A when nothing matches.- text
A2"Order 4471 for Contoso shipped today" - regular_expression
$E$1&" (.+?) "&$G$1"for (.+?) shipped"
- text
IFERROR(REGEXEXTRACT(A2,$E$1&" (.+?) "&$G$1),"")"Contoso"IFERROR: Returns a fallback when the value is any error.- value
REGEXEXTRACT(A2,$E$1&" (.+?) "&$G$1)"Contoso" - value_if_error
""not used
- value
In Excel
Other ways to do it
Each one was run on the same example sheet.
| Formula | Result | When to use it |
|---|---|---|
| =IFERROR(REGEXEXTRACT(A2,"(?i)"&$E$1&" (.+?) "&$G$1),"") | Contoso, Northwind Traders, Fabrikam… | Ignores case, so "FOR" and "Shipped" match too. |
Common mistakes
- A greedy
(.+)runs to the last "shipped" in the text instead of the first. - Words that contain pattern characters, like "Inc." or "(US)", must be escaped with a backslash.
Try it yourself
Same data, empty answer cell. Your formula is graded instantly, then re-checked on changed data, so a typed-in number won't pass.
Extract text between two words
B2, extract the text between the word in E1 and the word in G1, or leave it blank when they aren't there. Fill down to B6.Questions
How do I extract everything after a word?
Capture to the end: =REGEXEXTRACT(A2,"for (.+)$").
How do I trim spaces from the result?
Wrap it in TRIM: =TRIM(REGEXEXTRACT(A2,"for(.+?)shipped")).
Checked by running on 2026-10-30: every formula and result on this page was calculated by the Spreadsheet Practice formula engine.