mathspp
  • Blog
    • Pydon'ts
    • Problems
    • TIL
    • Twitter threads
  • Books
  • Talks
  • Trainings
    • Advanced iteration
    • Python for scripting and automation
    • Rust for Python developers
  • Courses
  • About
Link blog

Regex affordances

by Ned Batchelder on 09-05-2025 19:00

This short but instructional post by Ned covers some useful and not-so-common regular expression features, namely:

  • named groups with (?P<group_name>);
  • the verbose mode for regular expressions with the flag (?x); and
  • dynamic replacements with the function re.sub.

The regex with dynamic replacement was being used to replace references to $-prefixed variables. In the post, Ned wrote a function substitute_variables(text: str, variables: Mapping[str, str]) -> str that you could use like this:

print(
    substitute_variables(
        "Hey, my name is $name!",
        {"name": "Rodrigo"},
    )
)  # Hey, my name is Rodrigo!

Ned also shared a link to the function in its real context in coverage.py.

Previous link Next link

WolframAlpha helped me a lot when studying for Calculus...

mathspp
  • Blog
    • Pydon'ts
    • Problems
    • TIL
    • Twitter threads
  • Books
  • Talks
  • Trainings
    • Advanced iteration
    • Python for scripting and automation
    • Rust for Python developers
  • Courses
  • About