RegexReplace to Wrap Numbers in Brackets and Its Use in Query

With the help of the RegexReplace function, we can easily wrap numbers in a text string in brackets in Google Sheets.

This we can use to format the strings containing numbers. Other than this, there is one important use of adding brackets around numbers in Google Sheets.

As (partially) mentioned in the title, we can use the above Regex method to dynamically refer columns when using aggregation functions in Query.

You May Like: How to Sum, Avg, Count, Max, and Min in Google Sheets Query.

Let’s start this tutorial with how to use RegexReplace to wrap numbers in brackets in Google Sheets. The Query comes after that.

How to Add Brackets Around Numbers Using the RegexReplace Function in Google Sheets?

In my example below, I am using parentheses to wrap around the numbers. Instead, you can use square brackets too.

Syntax:

REGEXREPLACE(text, "(\d+)", "($1)")

Example 1:

The content of cell B2 is the text Purchase 100, Damage 50. The following RegexReplace formula in cell B3 will return the result; Purchase (100), Damage (50).

=REGEXREPLACE(B2,"(\d+)", "($1)")

To replace the parentheses around the numbers with square brackets, use the below formula.

=REGEXREPLACE(B2,"(\d+)", "[$1]")

See one more example.

Example 2:

This time the value in cell B2 is the number 100.

Here the above formula won’t work as the content in cell B2 doesn’t contain any text string!

You must use B2&"" instead of B2 to convert the number in cell B2 to text because RegexReplace is a text function and only takes text as the argument 1.

=REGEXREPLACE(B2&"","(\d+)", "[$1]")

This way we can use the RegexReplace function to wrap numbers in brackets in Google Sheets.

I have found one important use, or we can say real-life use, of this formula in the Query function. Let’s go to that.

RegexReplace to Wrap Numbers in Brackets and Its Query Select Clause Use

You have just learned how to use the RegexReplace function to wrap numbers in parenthesis (brackets) in Google Sheets.

Now let’s see how to use it in Query Select clause to dynamically refer to columns in Query aggregation functions.

Query Function ‘query’ – Non-Dynamic

Please see the image below for the sample data.

With the help of the Sum aggregation function in Query, we can sum the columns C, D, and E (Columns 2, 3 and 4 in the range B1:E).

=QUERY({B1:E},"Select Sum(Col2),Sum(Col3),Sum(Col4)",1)
REGEXREPLACE to Wrap Numbers in Brackets - Example

This normal Query formula has one drawback!

To know that, insert one column before column E. Only the range B1:E (data) will be automatically get adjusted within the formula.

Syntax: QUERY(data, query, [headers])

The ‘query’ argument is actually entered as a string (within double quotes). So it won’t get adjusted when the ‘data’ range changes.

Here I am going to make the ‘query’ argument dynamic with the help of our above RegexReplace formula.

Query Function ‘query’ – Dynamic

Here is the ‘query’ non-dynamic syntax.

"Select Sum(Col2),Sum(Col3),Sum(Col4)"

Using the below Sequence formula we can generate the column numbers to sum dynamically.

=sequence(1,columns(C1:E1),2)

The above Sequence formula in cell G1 adjusts based on new columns in the range.

Dynamic Aggregation Column in Google Sheets Query

Once again see the syntax of the RegexReplace formula that wraps numbers in brackets in Google Sheets. Here is that.

REGEXREPLACE(text, "(\d+)", "($1)")

In this, we need to replace/feed the ‘text’ argument with the above Sequence formula. But the Sequence formula output is a multi-column number. So it won’t work. Then?

As mentioned in the example 2, we must use &"" at the last part of the Sequence.

=REGEXREPLACE(sequence(1,columns(C1:E1),2)&"","(\d+)", "($1)")

Then wrap it with ArrayFormula since there is an array in use as ‘text’ in RegexReplace.

=ArrayFormula(REGEXREPLACE(sequence(1,columns(C1:E1),2)&"","(\d+)", "($1)"))

Output:

(2)(3)(4)

We have added brackets around numbers. I am slightly modifying the formula.

=ARRAYFORMULA(REGEXREPLACE(sequence(1,columns(C1:E1),2)&"","(\d+)", "Sum(Col$1)"))

Output:

Sum(Col2)Sum(Col3)Sum(Col4)

Then TextJoin these outputs.

=TEXTJOIN(",",TRUE,ARRAYFORMULA(REGEXREPLACE(sequence(1,columns(C1:E1),2)&"","(\d+)", "Sum(Col$1)")))

Result:

Sum(Col2),Sum(Col3),Sum(Col4)

Now, finally in the Query formula, I am replacing the ‘query’ with the above formula.

Added Brackets Around Numbers and Used in Query
=QUERY({B1:E},"Select "&TEXTJOIN(",",TRUE,ARRAYFORMULA(REGEXREPLACE(sequence(1,columns(C1:E1),2)&"","(\d+)", "Sum(Col$1)"))),1)

I hope you could learn the use of RegexReplace to wrap numbers in brackets and one of its real-life use with the help of Query.

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.

How to Break RANK Ties Alphabetically in Google Sheets

The RANK function in Google Sheets is commonly used to assign rankings to numerical...

Google Sheets: Highlight an Entire Column If Any Cell Has an Error

Google Sheets allows you to highlight an entire column if any cell has an...

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...

More like this

How to Break RANK Ties Alphabetically in Google Sheets

The RANK function in Google Sheets is commonly used to assign rankings to numerical...

Google Sheets: Highlight an Entire Column If Any Cell Has an Error

Google Sheets allows you to highlight an entire column if any cell has an...

Google Sheets: Extract Top N per Group from Query Aggregation

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

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.