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.

Adding a Dynamic Total Row to Excel FILTER Function Results

This tutorial introduces a unique formula to add a dynamic total row to your...

How to Apply Nested Column and Row Filters in Excel

Before we start applying nested column and row filtering using the FILTER function in...

Remove First Two Characters from a Cell in Excel – 3 Formulas

To remove the first two characters (whether they are letters or digits) from a...

Excel: Highlighting Parent and Child Rows or Columns

When highlighting parent and child rows or columns, I mean applying conditional formatting to...

More like this

Case-Sensitive Running Count in Google Sheets

This tutorial provides a solution for incorporating case sensitivity into the running count of...

Fix Fractional Percentage Formatting Issues in Google Sheets

Applying data validation is the only effective solution to resolve issues related to fractional...

Lookup the Smallest Value in a 2D Array in Google Sheets

We can use a combination of BYROW and SMALL with FILTER or XLOOKUP 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.