Google Sheets Regexreplace Function: How to Master It with Examples

Are you struggling with Google Sheets Regex Functions? You’re not alone! Many users find it challenging to navigate the world of Regex Functions without proper guidance. But fear not, because I’m here to help you understand and master one of the most powerful functions – Google Sheets Regexreplace. In this article, I will provide you with clear explanations and a variety of examples to help you grasp the concept and unleash the full potential of Regexreplace in Google Sheets. So, let’s dive in!

The Purpose of Google Sheets Regexreplace Function

The Regexreplace function is designed to replace a specific part of a text string with a different text string. To achieve this, Google Sheets utilizes RE2 for regular expressions. If you’re not familiar with RE2 regular expressions, I recommend bookmarking the regular expression syntax reference at GitHub for your convenience. It will come in handy whenever you face challenges while working with Regexreplace or other Regex functions in Google Sheets.

How to Use Regexreplace Function in Google Sheets

To start using Regexreplace, let’s understand its syntax:

REGEXREPLACE(text, regular_expression, replacement)
  • text: The text string in which you want to perform the replacement.
  • regular_expression: Use this to identify the matching instances within the text.
  • replacement: The new text string that will replace the matched instances in the original text.

Formulas to Learn Google Sheets Regexreplace Function

Let’s explore some practical formulas to demonstrate the versatility and power of Google Sheets Regexreplace. Assume the value in Cell A1 is “Info Inspired Inspirational 2017 Blog”.

1. Regexreplace Formula to Replace Any Plain Text

Formula:

=REGEXREPLACE(A1, "Inspirational "," - ")

Result: Info Inspired – 2017 Blog

2. Replace Any Number Using Regexreplace

Formula:

=REGEXREPLACE(A1, "[0-9]+","2018")

Result: Info Inspired Inspirational 2018 Blog

3. Regexreplace Formula to Remove All Spaces

Formula:

=REGEXREPLACE(A1, "s","")

Result: InfoInspiredInspirational2017Blog

When you want to replace spaces with a specific character, use the following formula. In this example, I’ve used a comma as the replacement character.

Formula:

=REGEXREPLACE(A1, "s",",")

Result: Info,Inspired,Inspirational,2017,Blog

4. Regexreplace Formula to Replace Any of the Character (This or That)

Formula:

=REGEXREPLACE(A1, "ob","ormation")

Result: Information Inspired Inspirational 2017 Blog

5. Regexreplace Formula to Replace the Last Word of a Sentence or a Certain Number of Characters from the End

You can either remove the last word by replacing it with empty space or add new text.

Formula (Remove the Last Word):

=REGEXREPLACE(A1, "....z","")

Result: Info Inspired Inspirational 2017

Formula (Replace with New Text):

=REGEXREPLACE(A1, "....z","Professional Blog")

Result: Info Inspired Inspirational 2017 Professional Blog

OR

Formula (Replace with New Text):

=REGEXREPLACE(A1, "....$","Professional Blog")

6. Regexreplace Formula to Replace a Certain Number of Characters or Words from the Beginning

Formula:

=REGEXREPLACE(A1, "^....","Newly")

Result: Newly Inspired Inspirational 2017 Blog

7. Regexreplace Formula to Remove All Numbers from a Text

Assume the value in Cell A2 is “I have paid USD 100 for my blog maintenance last month and USD 60 this month”.

Formula:

=REGEXREPLACE(A2, "[d]","")

Result: I have paid USD for my blog maintenance last month and USD this month

8. Remove or Replace All Numbers in a Text, Including Decimal Places

Assume the value in Cell A3 is “I have paid USD 100.25 for my blog last month and this month it’s USD 60.50”.

Formula:

=REGEXREPLACE(A3, "[d.]+", "")

or

=REGEXREPLACE(A3, "[0-9]*.[0-9]+[0-9]+","")

Result: I have paid USD for my blog last month and this month it’s USD

9. Remove or Replace Any Prefix to a Number Using Regexreplace

Assume the value in Cell A3 is “I have paid USD 100.25 for my blog last month and this month it’s USD 60.50”.

Formula:

=REGEXREPLACE(A3, "(w*)s[d.]+","")

Result: I have paid for my blog last month and this month it’s

10. Remove All Texts and Extract Numbers

Formula:

=REGEXREPLACE(A3, "[^d.]+", "|")

Result: |100.25|60.50

If there are no decimal places, the formula would be:

=REGEXREPLACE(A3, "[^d]+", "|")

11. Regex Formula to Replace Special Characters Around a Word or Text

Assume the value in Cell A4 is “Can you ~replace~ me!”.

Formula:

=REGEXREPLACE(A4, "~([A-Za-z]+)~","replace")

Result: Can you replace me!

Assume the value in Cell A5 is “Can you <replace> me!”.

Formula:

=REGEXREPLACE(A5, "&lt;([A-Za-z]+)&gt;","replace")

Result: Can you replace me!

or

Formula:

=REGEXREPLACE(A5, "&lt;(.+)&gt;","replace")

That’s all for now! Open a blank Google Sheet, apply the formulas mentioned above, and explore the RE2 syntax by experimenting with different expressions. With regular practice and experimentation, you’ll become an expert in Google Sheets Regex functions. And remember, if you need any further guidance, don’t hesitate to visit Crawlan.com for more resources and expert advice.

Happy Regexreplacing!

Related posts