Regex to Get All Words after Nth Word in a Sentence in Google Sheets

Are you tired of manually extracting words from a sentence in Google Sheets? Well, you’re in luck! With the power of regex, you can easily get all the words after the nth word in a sentence without breaking a sweat.

The Power of Regex in Google Sheets

In Google Sheets, we have a handy function called REGEXREPLACE that allows us to use regular expressions to manipulate text. By combining this function with other functions like QUERY, SPLIT, and SUBSTITUTE, we can achieve our desired result.

Let’s dive into the details and see how it works.

Extracting Words after the Nth Word

Imagine you have a complex formula that requires extracting all the words after the first word in a sentence. Instead of spending hours manually doing it, we can use a simple regex formula to get the job done.

Here’s how it works:

  1. We start by using the SUBSTITUTE function to replace the first occurrence of the space character with a unique delimiter, like a fish character.

=Query(split(SUBSTITUTE(A1," ","🐠",1),"🐠"),"Select Col2")

This formula extracts all the words after the first word in the sentence.

  1. To extract words after the nth word, simply replace the number 1 in the formula with the desired number.

For example, if you want to extract all the words after the fifth word, replace the number 1 with the number 5.

Isn’t it simple and elegant?

Making it Array-Friendly

But wait, there’s more! Our regex formula is not limited to extracting words from a single cell. It can be used with a cell range to handle multiple sentences at once.

Here’s an example:

=ArrayFormula(Query(split(SUBSTITUTE(A1:A100," ","🐠",1),"🐠"),"Select Col2"))

In this example, we use a cell range (A1:A100) instead of a single cell reference. Additionally, we include the ARRAYFORMULA function to expand the result.

Taking it to the Next Level with Regular Expressions

Now, if you want to take your regex skills to the next level, we have a regular expression-based formula that offers even more flexibility.

Regular Expression and Formula Options

Our regular expression looks like this:

^(w*W*){2}

Let’s break it down:

  • ^ asserts the position at the beginning of the text or line.
  • w matches word characters (underscore and alphanumeric characters [0-9A-Za-z_]).
  • * captures the preceding token between 0 and unlimited times.
  • W matches ‘not word’ characters (not underscore and alphanumeric characters [^0-9A-Za-z_]).
  • {n} matches the preceding token exactly n times.

With this regular expression, you can easily extract all the words after the nth word in a sentence.

To use this regular expression in a Google Sheets formula, simply replace A1 with the desired cell reference:

=REGEXREPLACE(A1,"^(w*W*){3}","")

The above formula will return all the words after the third word in the sentence.

Arrays and Regular Expressions

But wait, there’s more! Our regular expression formula also supports cell ranges, just like before.

Here’s an example:

=ArrayFormula(REGEXREPLACE(A1:A,"^(w*W*){2}",""))

In this example, the regular expression formula returns the last name from a list of names in column A. It replaces the first two words (the serial number and first name) with blank.

Conclusion

You’ve learned the power of regex in Google Sheets and how it can revolutionize the way you extract words after the nth word in a sentence. It’s a game-changer that saves you time and effort.

If you want to explore more regex formulas and unleash the true potential of Google Sheets, head over to Crawlan.com for more exciting tips and tricks.

So go ahead and give it a try. Your Google Sheets skills will never be the same again!

Enjoy and happy regexing!

[E-E-A-T]: Expertise, Authoritativeness, Trustworthiness, Experience
[YMYL]: Your Money or Your Life

Related posts