Given a string and a string template, how do I check if there is a valid variable that can be used as substitute?
For example,
def find_substitute(template: str, s: str) -> str:
...
template = "a_%(var)s_b_%(var)s_c"
find_substitute(template, "a_foo_b_foo_c") # Should return foo
find_substitute(template, "a_foo_b_bar_c") # Should raise, no valid substitution value
find_substitute(template, "a_foo_a_foo_a") # Should raise, no valid substitution value
I could do template.split("%(var)s")
and then try to match each section of the string, but I'm guessing there's a better way to do this, perhaps using regex.