def repl(m):
    s = m.group(1)
return d[s]
s = '/(?P<something>\\w+)/whatever/(?P<something2>\\w+)'
p = re.compile('\\(\\?P<(.*?)>\\\\w\\+\\)')
print(p.sub(repl, s))
How to compose string from regex pattern with named groups and datadict in python?