How to Use REGEXREPLACE and Pattern Substitution in Microsoft Excel

Standard data cleansing in Microsoft Excel has long relied on the SUBSTITUTE and REPLACE functions. However, both functions suffer from critical limitations: SUBSTITUTE requires an exact literal string match, and REPLACE requires fixed, hardcoded character position counts. When dealing with inconsistent data entries—such as varying phone number formats, irregular whitespace, or mixed alphanumeric codes—traditional formulas require dozen-layer nested substitutions. In Microsoft 365 Excel, Microsoft introduced REGEXREPLACE, empowering modelers to execute regex pattern matching and backreference substitutions effortlessly.

Syntax and Parameters of REGEXREPLACE

The REGEXREPLACE function searches a text string for matches against a regular expression and replaces them with a designated replacement string:

=REGEXREPLACE(text, pattern, replacement, [occurrence], [case_sensitivity])
  • text: The source cell or text to modify (e.g., A2).
  • pattern: The regular expression to search for (e.g., "[^0-9]").
  • replacement: The string to substitute in place of matching patterns (supports backreferences like $1, $2).
  • [occurrence]: Optional integer specifying which instance to replace (0 = replace all occurrences, 1 = replace first only).
  • [case_sensitivity]: Optional flag (0 = case-sensitive, 1 = case-insensitive).

Stripping Non-Numeric Characters from Raw Inputs

When scrubbing messy phone numbers, credit card strings, or product barcodes containing spaces, dashes, and parentheses:

# Strip everything except numerical digits (0-9)
=REGEXREPLACE(A2, "[^0-9]", "")

This single formula instantly transforms inputs like "+1 (555) 234-5678" into clean database integers: "15552345678".

Standardizing Phone Number Formats Using Capture Groups

Beyond stripping characters, REGEXREPLACE supports capture group backreferences ($1, $2, etc.) to reorder and reformat data:

# Reformat 10-digit strings into standardized (XXX) XXX-XXXX format
=REGEXREPLACE(A2, "^(d{3})(d{3})(d{4})$", "($1) $2-$3")

Sanitizing Irregular Whitespace and Line Breaks

Text copied from PDF tables or web pages frequently contains multiple consecutive spaces, non-breaking spaces, and tab characters:

# Collapse multiple consecutive whitespace characters into a single space
=REGEXREPLACE(A2, "s+", " ")

# Remove all leading and trailing whitespace along with non-printable characters
=TRIM(REGEXREPLACE(A2, "[
	]+", " "))

Anonymizing Sensitive PII Data (Masking Credit Cards & SSNs)

To mask customer Social Security Numbers or credit card records for external reporting while preserving the last 4 digits:

# Mask SSN format (XXX-XX-1234)
=REGEXREPLACE(A2, "^d{3}-d{2}-(d{4})$", "XXX-XX-$1")

By replacing brittle multi-step helper columns with declarative regex patterns, REGEXREPLACE streamlines data transformation pipelines across Microsoft Excel.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.