Extract Last N Values from a Delimiter Separated String in Google Sheets

With or without using RE2 regular expressions, we can extract the last n values from a delimiter (comma, pipe, tide, asterisk, etc.) separated string in Google Sheets.

If you don’t want to use a regular expression (regex function), to extract the last n values from the end of a string, you can follow the below steps (method).

  1. Count to the nth occurrence of the delimiter present in your string from the end. Mark to split by replacing it with another delimiter.
  2. Split the string by this replaced delimiter.
  3. Extract the second column (second value) of the split string.

I’ve already detailed the above point # 1 and # 3 in my earlier two tutorials. Here are them.

Regarding point # 2, it’s just about using the Split function.

First, let’s see how to extract the last n values from a string as above (without using a regular expression) in Google Sheets. Then you will get the more simple regex formula.

Extract Last N Values without Using Regular Expression in Google Sheets

In cell B2, insert the below comma separated names. Do not include the double quotes.

“Shawn, Ruby, Grant, Jorge, Ismael, Ray, Norma, Darryl”

I’m going to write a formula to extract the last 3 names from this comma delimiter separated list. They are the names “Ray, Norma, Darryl“.

The below formula as per point # 1 above will replace the 3rd , delimiter from the end of the string with a > sign.

=substitute(B2,",",">",LEN(B2)- LEN(SUBSTITUTE(B2,",",""))-2)

Using the Split function (point # 2) we can split the above output into two columns.

=split(substitute(B2,",",">",LEN(B2)- LEN(SUBSTITUTE(B2,",",""))-2),">")

Finally extract the second column as per point # 3. That will be the last 3 values.

=index(split(substitute(B2,",",">",LEN(B2)- LEN(SUBSTITUTE(B2,",",""))-2),">"),0,2)

Result:

“Ray, Norma, Darryl”

Additionally, you can wrap the above formula with Trim to remove extra white space in the result. I didn’t use that function though.

We have extracted the last n (here 3) values without using a regular expression in Google Sheets.

Instead of value in cell B2, what about multiple commas or any other delimiter separated lists in a range?

See the values in B2:B4. We can use the same above formula to extract the last n values from the strings in the range. Only change the reference from B2 to B4.

=index(trim(split(substitute(B2:B4,",",">",LEN(B2:B4)- LEN(SUBSTITUTE(B2:B4,",",""))-2),">")),0,2)
Extract Last N Values from a String in Google Sheets

This time I’ve included Trim with this array formula. It’s not necessary though.

You may have noticed that the number of values separated by the comma delimiter is different in one of the rows.

There are eight names each in cell B2 and B3. But in cell B4 there are nine names. Still, my formula is able to extract the last n values correctly from each row.

Now to the simple regex variant of the above formula. Here also I’ll provide you array and non-array formulas.

Extract Last N Values Using Regular Expression in Google Sheets

When you come to regular expressions, there are 4 supporting functions in Google Sheets. They are Query, Regexmatch, Regexextract, and Regexreplace. We will use the Regexextract function here.

I know some of you may be surprised to see the Query in the list of functions. I’ve included Query considering the Matches string comparison operator which takes regular expressions in it.

How to use Regexextract to extract the last n values from a delimiter separated string in Google Sheets?

See the below example.

In the earlier examples, the values that we have used to test were in B2:B4. This time the same values are in B8:B10.

=REGEXEXTRACT(B8,"((?:[^\,]+\,*){3})$")

The above Regexextract formula would extract the last 3 words from the end of the comma-separated value in cell B8.

Regarding the capturing group ((?:[^\,]+\,*){3})$ ;

?: – non-capturing group, i.e. (?:[^\,]+\,*){3}.
[^\,] – match a single character not present in the set. + (quantifier).
\, – match the character ,. * (quantifier).
{3} – matches 3 times.
$ – at the end of the string.

For B8:B10, we can use it as an array formula using the ArrayFormula function with Regexextract as below.

=ArrayFormula(TRIM(REGEXEXTRACT(B8:B10,"((?:[^\,]+\,*){3})$")))

This time also I’ve included Trim to remove the white space at the beginning of the extracted values.

Regexextract N Values from the End of a String

Additional Tip

Can you explain how to remove n values counting from the end of a delimiter separated list?

Yes! For example, instead of extracting the names “Ray, Norma, Darryl” from cell B8, we can remove these names and extract the other names.

I mean we can extract the names “Shawn, Ruby, Grant, Jorge, Ismael”. For this, we can use Regexreplace instead of Regexextract as below.

=REGEXREPLACE(B8,"((?:\,[^\,]*){3})$","")

That’s all. Enjoy!

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.

Filter Top N per Category in Excel (Using FILTER & COUNTIFS)

You can use a combination of FILTER and COUNTIFS to filter the top N...

Rank per Group in Excel

You have two groups of 20 students each. How do you determine the rank...

Real-Time Excel Filtering Using Combo Box & FILTER Function

In this tutorial, you’ll learn how to set up real-time Excel filtering using a...

Google Sheets: Extract Top N per Group from Query Aggregation

When working with grouped and aggregated data in Google Sheets, you might need to...

More like this

Google Sheets: Extract Top N per Group from Query Aggregation

When working with grouped and aggregated data in Google Sheets, you might need to...

How to Extract Top N from Aggregated Query Results in Google Sheets

To summarize data in Google Sheets, you can use the QUERY function. However, if...

How to Use RANK IF in Google Sheets (Conditional Ranking)

You can use the RANK function to rank values in an entire dataset. But...

2 COMMENTS

  1. Thanks, this was super helpful! That was very clever to count the delimiters and substitute a specific delimiter based on that count. I don’t think that ever would have occurred to me. It’s really too bad that google sheets won’t let you specify index numbers relative to the end of an array, but this is a great workaround.

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.