Skip to main content
PYSPARK โ€ข LESSON 56

Pattern Replacement with regexp_replace()

How do we strip all non-numeric characters from customer phone numbers using regular expressions?

Intermediateโฑ 3 Minutesโญ 370 XP
๐Ÿค” THE QUESTION

How do we strip all non-numeric characters from customer phone numbers using regular expressions?

๐Ÿ’ก WHAT IS IT?

regexp_replace() matches a regex pattern and replaces all occurrences with a replacement string.

๐ŸŽฏ WHAT IS IT USED FOR?

Phone number standardization, removing special characters, and data masking for privacy compliance.

๐Ÿ’ป EXAMPLE
from pyspark.sql.functions import col, regexp_replace

df = df.withColumn("clean_phone", regexp_replace(col("phone"), "[^0-9]", ""))

๐ŸŽฏ Mission Objectives

Practice typing production-grade PySpark code for Pattern Replacement with regexp_replace().

  • โœ“ Import regexp_replace function
  • โœ“ Apply regex pattern matching non-digits
  • โœ“ Sanitize phone number strings