HomeGoogle DocsSpreadsheetHow to Replace Every Nth Delimiter in Google Sheets

How to Replace Every Nth Delimiter in Google Sheets

Sometimes you may need to replace every nth delimiter in a string in Google Sheets — whether it’s a comma, an asterisk, or another separator. This can be handy for grouping items, formatting text for better readability, or even creating dynamic formulas like QUERY strings.

Fortunately, Google Sheets offers powerful functions like REGEXREPLACE that let you do this without manual edits.

Why Replace Every Nth Delimiter?

Here are a few scenarios where replacing every nth delimiter can be useful:

  • Grouping data — e.g., grouping every 2 or 3 names together.
  • Formatting formulas — like turning a repetitive multiplication pattern into an addition formula.
  • Data preparation — breaking or marking sections in a long delimited string for later splitting.

Example 1: Replace Every Nth Delimiter for Grouping

Let’s start with a basic use case — grouping every 2 names from a comma-delimited string.

Sample data (A2):

Ben, John, Samuel, Megan, Joel, Edith, Diane, Teresa, Helen, Frank, Pauline, Mike

We want to create groups of two names separated by |:

Formula:

=REGEXREPLACE(
  REGEXREPLACE(A2,"(\,.*?){2}","$0|"),
  "\,\|"," |"
)

Output:

Ben, John | Samuel, Megan | Joel, Edith | Diane, Teresa | Helen, Frank | Pauline, Mike

Note: In the formula, the number inside { } (here {2}) represents the nth delimiter to replace. For example, use {3} to target every 3rd delimiter, {4} for every 4th, and so on.

Now, if you want to split these groups into rows:

=ARRAYFORMULA(TRIM(TRANSPOSE(SPLIT(
REGEXREPLACE(
  REGEXREPLACE(A2,"(\,.*?){2}","$0|"),
  "\,\|"," |"
), " |", FALSE))))

Output:

Ben, John
Samuel, Megan
Joel, Edith
Diane, Teresa
Helen, Frank
Pauline, Mike

Pro Tip:
For grouping specifically, you can also skip regex and use WRAPROWS like this:

=ARRAYFORMULA(
 LET(group, WRAPROWS(SPLIT(A2, ", "), 2),
 CHOOSECOLS(group, 1)&", "&CHOOSECOLS(group, 2))
)

Example 2: Replace Every Nth Delimiter to Create a Dynamic QUERY String

Now let’s look at a more advanced example — creating a dynamic aggregation part of a QUERY formula.

Sample Data

ItemVendor A PriceVendor A QtyVendor B PriceVendor B QtyVendor C PriceVendor C Qty
Apple$4.0010$4.005$4.005
Orange$3.7520$3.7520$3.7510
Mango$4.0030$4.0030$4.0015

The standard query might look like:

=QUERY(A1:G, "SELECT Col1, Col2*Col3+Col4*Col5+Col6*Col7 LABEL Col2*Col3+Col4*Col5+Col6*Col7 'Total'", 1)
Google Sheets formula showing dynamic SEQUENCE adjustment for varying vendor price and quantity pairs

Instead of typing Col2*Col3+Col4*Col5+Col6*Col7 manually, we can generate it dynamically.

Step 1: Generate a Multiplication String

=ARRAYFORMULA(TEXTJOIN("*",TRUE,"Col"&SEQUENCE(1,6,2)))

Output:

Col2*Col3*Col4*Col5*Col6*Col7

Step 2: Replace Every Second * with a +

Here’s where we use REGEXREPLACE to replace every nth delimiter (in this case, every 2nd asterisk) with a plus sign:

=REGEXREPLACE(
  REGEXREPLACE(
    ARRAYFORMULA(TEXTJOIN("*",TRUE,"Col"&SEQUENCE(1,6,2))),
    "(\*.*?){2}","$0+"
  ),
  "\*\+","+" 
)

Output:

Col2*Col3+Col4*Col5+Col6*Col7

Step 3: Embed in the QUERY

=LET(
  string,
  REGEXREPLACE(
    REGEXREPLACE(
      ARRAYFORMULA(TEXTJOIN("*",TRUE,"Col"&SEQUENCE(1,6,2))),
      "(\*.*?){2}","$0+"
    ),
    "\*\+","+" 
  ),
  QUERY(A1:G, "SELECT Col1, "&string&" LABEL "&string&" 'Total'", 1)
)

Now you’ve got a fully dynamic formula — just update the sequence range, and it will work even if the number of vendor price/quantity pairs changes.

Conclusion

Using the Replace Every Nth Delimiter technique in Google Sheets opens up a lot of automation possibilities.

  • For grouping, it makes long lists much easier to process.
  • For dynamic formulas, it saves time and reduces the chance of manual errors.
  • With REGEXREPLACE and functions like TEXTJOIN, and SEQUENCE, you can tackle almost any delimiter manipulation task.

Whether it’s commas, pipes, or asterisks, the approach is flexible and reusable — and once you master it, you’ll find yourself applying it in all sorts of creative ways.

Related Resources

Prashanth K V
Prashanth K V
Your Trusted Google Sheets and Excel Expert Prashanth K V 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.

Top Discussions

More like this

How to Use the SHEET and SHEETS Functions in Google Sheets

The SHEET and SHEETS functions let you retrieve information about worksheets in a Google...

How to Create a Self-Healing Table of Contents in Google Sheets

A table of contents makes navigating large Google Sheets workbooks much easier. However, a...

Sort a Tab Name List Dynamically by Workbook Order in Google Sheets

When your workbook contains many sheets (tabs), you may create a table of contents...

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.