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?

Intermediate3 Minutes370 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