Split a Text after Every Nth Word in Google Sheets (Using Regex and Split)

Published on

Using Regexreplace and Split, it is to easy to split a text string after every nth word in Google Sheets.

Assume, you have imported or copy-pasted a long text or sentence into a cell in Google Sheets.

For data processing or for some other purpose, you may want to split that long text in a cell into multiple rows.

For example, see this Lorem Ipsum place holder text in cell B4.

Long sample text in a cell

I have wrapped the string and due to wrapping, the row width got increased to accommodate the full text.

I don’t want to increase the width of the row, still want to read it. If so, instead of wrapping the text, what I want is to split the text after every nth word.

Example to split a text after every nth word in Google Sheets

As you can see in the example, the text in cell B4 is split into multiple rows and each row is containing 5 words.

How to split a text in a cell after every nth word as above in Google Sheets?

As mentioned in the title, for this, we can use the REGEXREPLACE and SPLIT functions in Google Sheets. Here is how.

Logic:

  1. Insert a fish delimiter after every nth word – using REGEXREPLACE.
  2. Split the text at fish delimiters – using the SPLIT.
  3. Finally, change the split columns to row – using TRANSPOSE.

Regexreplace Formula to Insert Delimiters at Every Nth Word

Our long text is in cell B4. So let’s write the Regexreplace in cell D4.

=REGEXREPLACE(B4,"(([a-zA-Z'‘’:.,_?!”“)(}{\]\[]+\s){5})", "$1🐟")

The above is the Regexreplace formula to insert a fish delimiter after every 5th word.

In the formula, please see the number within the curly braces. That number determines the nth.

Do you want an explanation to the above formula? Then first see the syntax of the function in use in the formula.

Syntax: REGEXREPLACE(text, regular_expression, replacement)

Actually in the formula the regular_expression is as simple as this.

"(([a-zA-Z]+\s){5})"

But in all the cases, it may not correctly place the delimiter after every nth word.

That means, if your long string contains special characters such as a period, hyphen, exclamation, brackets, etc., then the above simple regular_expression may falter.

So I have included a few of the special characters within the regular expression to solve the issue.

In addition to that instead of B4, you can use clean(B4) to clean extra spaces in the text in cell B4 (not used in my formula).

Fish delimiter after every nth word

The result looks cool because of the fish delimiter, right?

Split the Text after Every Nth Word Based on the Fish Delimiter in Google Sheets

Since we have correctly placed the fish delimiters, we can effectively use the Split function.

Let’s wrap the D4 formula with the Split. This will place the split text in the same row in multiple columns.

=split(REGEXREPLACE(B4,"(([a-zA-Z'‘’:.,_?!”“)(}{\]\[]+\s){5})", "$1🐟"),"🐟")

So additionally use the Transpose function with the Split + Regex to change the data orientation from row to column.

=TRANSPOSE(split(REGEXREPLACE(B4,"(([a-zA-Z'‘’:.,_?!”“)(}{\]\[]+\s){5})", "$1🐟"),"🐟"))

The above is the final formula to split a text after every nth (here 5th) word in Google Sheets.

You can dynamically control the position to split by pointing the nth in to a cell. Instead of hardcoding you can type 5 in cell C4 and refer to that in the formula.

For that replace {5} in the formula with {"&C4&"}.

This way we can split a text after every nth word in Google Sheets.

Thanks for the stay. Enjoy!

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Expert Prashanth KV is a Diamond Product Expert in Google Sheets, officially recognized by Google for his contributions to the Docs Editors Help Community and featured in the Google Product Experts Directory. Explore his blog to learn advanced formulas, automation tips, and problem-solving techniques to elevate your spreadsheet skills.

Custom Order for QUERY Pivot Headers in Google Sheets

By default, when you use the PIVOT clause in a Google Sheets QUERY, the...

Calculate Trip Days by Month (Start, End, and Full Days) in Google Sheets

If you're managing business travel in Google Sheets, you may need to calculate how...

Mode of Comma-Separated Numbers in Excel (Dynamic Array)

There is no dedicated function in Excel to directly find the mode of comma-separated...

How to Find Mode of Comma-Separated Numbers in Google Sheets

Working with comma-separated numbers inside a single cell is a common scenario in Google...

More like this

Custom Order for QUERY Pivot Headers in Google Sheets

By default, when you use the PIVOT clause in a Google Sheets QUERY, the...

Calculate Trip Days by Month (Start, End, and Full Days) in Google Sheets

If you're managing business travel in Google Sheets, you may need to calculate how...

How to Find Mode of Comma-Separated Numbers in Google Sheets

Working with comma-separated numbers inside a single cell is a common scenario in Google...

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.