re.sub()
String & Text Processing

Meaning

Replaces all non-overlapping occurrences of a regex pattern in a string with a specified replacement. It addresses the pain point of needing pattern-based text transformation where literal matching is insufficient. Reach for this when string data contains variable formats or structures that must be normalized, redacted, or reformatted.

Primary Function

Data transformation

Communicative Purpose

Enables pattern-based text substitution where literal string replacement is insufficient.

Pattern

re.sub(pattern, repl, string, count=max_replacements, flags=regex_flags)

Core Structure

re.sub(..., ..., ...)

Função primária

Data transformation

Propósito comunicativo

Enables pattern-based text substitution where literal string replacement is insufficient.

Situações de gatilho

Data cleaning: normalizing inconsistent phone number formats in a dataset. Log parsing: redacting sensitive information like emails or IPs from log files. Web scraping: extracting and reformatting date strings from raw HTML text.

Contextos

Text processing, data cleaning, log parsing, web scraping, configuration templating, and any scenario requiring pattern-based string replacement.

Padrão

re.sub(pattern, repl, string, count=max_replacements, flags=regex_flags)

Estrutura central

re.sub(..., ..., ...)

Slots de substituição

pattern: str (regex), repl: str or callable, string: str (input text), count: int ≥ 0, flags: int (regex flags)

Colocados típicos

  • re
  • compile
  • raw string r'...'
  • flags
  • lambda
  • match object
  • str.replace
  • re.subn

Substituições comuns

  • re.subn (returns count)
  • str.replace (literal)
  • re.sub with lambda repl
  • using a compiled regex object via .sub

Erros comuns

Forgetting raw string prefix for pattern (cause: misunderstanding escape sequences; consequence: regex fails or throws invalid escape error). Using str.replace for regex patterns (cause: confusing literal and pattern matching; consequence: dynamic patterns fail to match). Forgetting to import re (cause: oversight; consequence: NameError at runtime).

Similar / contraste

str.replace (literal replace), re.subn (returns (new_string, count)), re.sub with a compiled pattern, re.sub with a lambda or function as repl.

Interferências

Coming from JavaScript: expecting replacement string to use $1 for group references → Python uses \1 or \g<1> for backreferences. Coming from sed: expecting the replacement to modify the string in-place → re.sub returns a new string and does not mutate the original.

Família do chunk

  • Regex substitution functions

Nuance

1. Do not use when replacing a fixed literal substring; str.replace is faster and avoids regex overhead. 2. Compiling the regex first with re.compile is more efficient if the same pattern is used repeatedly in a loop. 3. Overlapping matches are not replaced; only the leftmost match of any overlapping set is handled.

Efeito pragmático

Enables dynamic, pattern‑based text transformation, facilitating data cleaning, templating, and format conversion.

Dica de memória

Think ‘substitute’ – replace matches with a replacement.

Nota

Remember to import re and consider using raw strings (r'...') for patterns to avoid unintended escape sequences.

Upgrade path

Learn re.subn for getting the replacement count, then re.compile for performance, and finally explore advanced regex features like lookarounds and conditional patterns.

Frequência: HighFormulaicidade: Semi-fixedTipo de construção: Function callPrioridade de aquisição: Active recallPrioridade de output: BothTag de espaçamento: Immediate

Log in to save chunks.