Extracting Strings in Google Sheets with REGEXEXTRACT and REGEXREPLACE

Apr 05,2025

vlogize

2016-11-23T10:23:24Z

Learn how to extract a substring up to a specified sequence in Google Sheets using `REGEXEXTRACT` and `REGEXREPLACE` functions effectively.
---
This video is based on the question https://stackoverflow.com/q/77930907/ asked by the user 'Armidale Storeperson' ( https://stackoverflow.com/u/23339860/ ) and on the answer https://stackoverflow.com/a/77930912/ provided by the user 'z..' ( https://stackoverflow.com/u/17887301/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.

Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: RE2 expression to extract string up to the sequence " :" if present

Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/licensing
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license.

If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
How to Extract Strings Up to a Specific Sequence in Google Sheets

If you are working with textual data in Google Sheets, you may encounter instances where you need to clean up your data by removing unwanted portions of strings. One such challenge is removing everything after a specific sequence in a text, such as " :". In this guide, we will explore how to effectively achieve this using Google Sheets' powerful regular expression functions: REGEXEXTRACT and REGEXREPLACE, as well as an alternative method for those who might prefer not to use regex.

The Problem Statement

You have a column of text strings that may contain data followed by " :", and your goal is to extract only the portion of the string that exists before this sequence. Let's say your data includes the following examples:

Sydney Distr Center

Port Macquarie

Port Macquarie : Port Macquarie Display

Taree

Taree : Taree Display

The desired output should be:

Sydney Distr Center

Port Macquarie

Port Macquarie

Taree

Taree

Using the regex pattern ^[^\s:]+ was close, but it returned only the first word of each string. This is because it does not account for spaces in the text.

The Solution

Option 1: Using REGEXEXTRACT

The REGEXEXTRACT function is a great way to extract text using a regex pattern. Here’s how to use it for our problem:

[[See Video to Reveal this Text or Code Snippet]]

Explanation:

A1 & " :" appends " :" to the string in cell A1.

"(.+ ?) :" captures everything before " :" (including spaces) in a non-greedy manner.

Option 2: Using REGEXREPLACE

If you prefer to replace unwanted portions instead, REGEXREPLACE can be very helpful:

[[See Video to Reveal this Text or Code Snippet]]

Explanation:

(.+ ) :.+ matches everything leading up to " :" and everything after that.

"$1" replaces the matched text with only the captured group before the sequence.

Option 3: Non-regex Alternative

If you want a simpler approach without regular expressions, you can achieve the same result using the SPLIT function in combination with INDEX:

[[See Video to Reveal this Text or Code Snippet]]

Explanation:

SPLIT(A1, " :") will divide the original string at " :".

INDEX(..., 1) returns the first part of the split string.

Conclusion

In this guide, we've seen various methods to extract strings up to a specific sequence in Google Sheets. Whether you choose the flexibility of regex through REGEXEXTRACT and REGEXREPLACE, or the simplicity of the SPLIT function, you can effectively achieve your goal of cleaning up text data. Choose the method that best fits your needs, and simplify your data processing in Google Sheets!

If you have additional questions or need further assistance with Google Sheets functions, feel free to leave a comment below.

RE2 expression to extract string up to the sequence : if presentgoogle sheetsre2