How to Match | Extract Nth Word in a Line in Google Sheets

Published on

The easiest way to match the nth word in a line in a cell in Google Sheets is by using the REGEXMATCH function. What about extracting the nth word?

To extract the nth word from a sentence/line in Google Sheets, we can depend on the REGEXEXTRACT function.

Further, in both the cases (extract/match), we can use a SPLIT + INDEX combo formula.

First of all, let’s learn how to extract the nth word from a sentence or line in a cell in Google Sheets. Then we can move to match the nth word in Google Sheets, as in the match also we need to extract the nth word first.

Extract Nth Word from a Sentence in Google Sheets

In my sheet, cell A2 contains the idiom “Let the cat out of the bag”. I’ll show you how to extract each word from this line in Google Sheets.

As a side note, I have chosen this idiom without any rhyme or reason. My intention is to just to make you understand how to extract the nth word from a line/sentence in Google Sheets. You can test my formula with your real-life data.

Using SPLIT + INDEX Combo (Non-Array Formula)

First I am giving you the generic SPLIT + INDEX combo formula to extract the nth word from the said line (idiom) in Google Sheets.

Syntax:

=index(split(line_to_extract," "),1,n)

Just change line_to_extract with cell A2 and ‘n’ with the word number in the order.

For example, “cat” is the third word in the sentence/idiom in cell A2. So to extract the word “cat”, use the below formula.

=index(split(A2," "),1,3)

Formula Explanation

The SPLIT formula splits the words in the sentence/line into columns. The space (delimiter) in the sentence enables us to split the words.

split(A2," ")

Split Result in Columns:

There are 7 words, so the output will be in 7 columns.

Letthecatoutofthebag

We want to extract the 3rd word from these. The INDEX formula offsets three columns and returns the word “cat”.

We can use the above result (formula itself) to match the nth (here 3rd) word in a line (here idiom) in Google Sheets. But before going to that, here is one more formula to extract.

Using REGEXEXTRACT (Non-Array Formula)

Here is the REGEXEXTRACT equivalent for extracting the 3rd word from the line/sentence.

=REGEXEXTRACT(A2&" ","^(\w+\s){3}")

The number 3 within the Curly Braces (also called Curly Brackets) controls the nth position of the words. That means to extract the 2nd word, use the number {2} instead.

The above are the two different methods to extract nth word from a line/sentence in Google Sheets.

Array Formula to Extract Nth Word in Google Sheets

We can use the REGEXEXTRACT function to extract nth word from a range (array). But, the SPLIT + INDEX won’t work in an array. Instead, we can replace the INDEX with QUERY.

Formula 1:

The following formula in Google Sheets would extract all the 2nd words from the range A2:A since we have used Col2 in the ‘Select’ clause in the QUERY formula.

=ArrayFormula(Query(split(A2:A," "),"Select Col2"))
Array Formula to Extract Nth Word from a Line in Google Sheets

Use Col3 to extract all the third words.

Here is the above array formula equivalent using REGEXEXTRACT.

Formula 2:

=ArrayFormula(if(A2:A="",,REGEXEXTRACT(A2:A&" ","^(\w+\s){2}")))

That’s all about how to extract the nth word from a line in a cell (also in an array) in Google Sheets.

REGEXMATCH to Match Nth Word in a Sentence in Google Sheets

Here we can use either of the REGEXEXTRACT or SPLIT + INDEX formulas within REGEXMATCH to match the nth word in a line in Google Sheets.

I am skipping the SPLIT + QUERY because I’m not going to explain array formula usage this time as it may not come in use in real-life.

If you are looking for an array formula to match the nth word in Google Sheets, please let me know in the comments. I’ll try to help you out. Now let’s come back to the example.

We have the earlier said idiom in cell A2. I just want to test whether the third word is “cat” in that idiom.

Steps:

Extract the third-word using REGEXEXTRACT or SPLIT + INDEX combo as explained above and TRIM it. I am using the former one.

=trim(REGEXEXTRACT(A2&" ","^(\w+\s){3}"))

Then REGEXMATCH the word “cat” with the extracted word, I mean the extract formula itself.

=REGEXMATCH("cat",trim(REGEXEXTRACT(A2&" ","^(\w+\s){3}")))

The formula would return TRUE if it matches, else FALSE.

Match Nth Word in a Sentence in Google Sheets

Resources

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Guide Prashanth KV brings a wealth of experience in Google Sheets and Excel, cultivated through years of work with multinational corporations in Mumbai and Dubai. As a recognized Google Product Expert in Docs Editors, Prashanth shares his expertise through insightful blogging since 2012. Explore his blog for practical tips and guidance on maximizing your spreadsheet skills.

Highlight Upcoming Birthdays in Google Sheets

When highlighting upcoming birthdays in Google Sheets, one important aspect is ignoring the year...

Compare Two Tables for Differences in Excel

To compare two tables with similar data for differences, you can use the XLOOKUP...

Calculate Weighted Average in Pivot Table in Google Sheets

You can calculate a weighted average in a Pivot Table using a custom formula...

Summarize Data and Keep the Last Record in Google Sheets

In Google Sheets, we can summarize data (like sum, average, min, max, count) using...

More like this

Highlight Upcoming Birthdays in Google Sheets

When highlighting upcoming birthdays in Google Sheets, one important aspect is ignoring the year...

Calculate Weighted Average in Pivot Table in Google Sheets

You can calculate a weighted average in a Pivot Table using a custom formula...

Summarize Data and Keep the Last Record in Google Sheets

In Google Sheets, we can summarize data (like sum, average, min, max, count) using...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.