This short but instructional post by Ned covers some useful and not-so-common regular expression features, namely:
(?P<group_name>)
;(?x)
; andre.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
.