Sub topic 12
Strip unwanted characters with REGEXREPLACE
Prerequisites and the cleanup problem
This chapter assumes that you can select ranges, enter formulas, and use basic Google Sheets menus. It also assumes that your data is on a copy of the original sheet. Keep the imported values unchanged so that you can compare your cleaned results with the source.
Messy text often contains characters that do not belong in the value you want to analyze. Phone numbers may contain parentheses, spaces, and hyphens. Product codes may contain inconsistent punctuation. A column may contain values such as:
| Original value | Desired value |
|---|---|
| (555) 010-1234 | 5550101234 |
| SKU: AB-204 | AB204 |
| 42 / 7 | 427 |
REGEXREPLACE is a Google Sheets function that finds text matching a pattern and replaces it with other text. A regular expression, or regex, is a compact pattern that describes characters or positions in text.
Remove characters by describing what to keep
The basic form is:
=REGEXREPLACE(text, regular_expression, replacement)To remove unwanted characters, replace every match with an empty string, written as "". The pattern [^0-9] means “any character that is not a digit.”
=REGEXREPLACE(A2, "[^0-9]", "")If A2 contains (555) 010-1234, the result is 5550101234.
This approach is safer than listing every possible punctuation mark. It keeps all digits, including digits you did not anticipate, and removes everything else.
To keep letters, digits, and spaces, use a different character class:
=REGEXREPLACE(A2, "[^A-Za-z0-9 ]", "")This removes punctuation but keeps ordinary spaces. If you also want to collapse extra spaces, run TRIM around the result:
=TRIM(REGEXREPLACE(A2, "[^A-Za-z0-9 ]", ""))Clean a whole column
Place a formula in an empty helper column. ARRAYFORMULA applies the operation to many rows at once. The IF condition prevents blank rows from producing unwanted results.
=ARRAYFORMULA(IF(A2:A="", "", REGEXREPLACE(A2:A, "[^0-9]", "")))After checking the output, you can copy the results and use Edit > Paste special > Values only if you need fixed values instead of formulas.
Combine REGEXREPLACE with other cleanup steps
A cleanup pipeline applies several transformations in a deliberate order. For example, this formula removes non-printing characters, converts non-breaking spaces to ordinary spaces, trims the result, and removes characters other than letters, digits, or spaces:
=ARRAYFORMULA(IF(A2:A="", "", TRIM(REGEXREPLACE(CLEAN(SUBSTITUTE(A2:A, CHAR(160), " ")), "[^A-Za-z0-9 ]", ""))))Use REGEXREPLACE after splitting combined values when each resulting column needs a different rule. For example, clean a phone-number column by keeping digits, but clean a name column by keeping letters and spaces.
Check the result
Compare a sample of cleaned values with the original rows. Look for lost decimal points, meaningful hyphens, letters with accents, and values that became blank. If the cleaned column will be used in lookups, test a few known matches after cleanup.
Exercise
What formula removes every character except digits from the value in A2?
Show answer
=REGEXREPLACE(A2, "[^0-9]", "")