For a string
s = '{{a,b}} and {{c,d}} and {{0,2}}'
I'd like to replace every {{...}} pattern by randomly one of the items in the list inside, i.e.:
"a and d and 2"
"b and d and 0"
"b and c and 0"
...
I remember that there's a way in module re to not simply replace like re.sub, but have a custom replacement function, but I can't find this anymore in the doc (maybe I'm searching with wrong keywords...)
This doesn't give any output:
import re
r = re.match('{{.*?}}', '{{a,b}} and {{c,d}} and {{0,2}}')
for m in r.groups():
print(m)
re.subdocs have an example of using a function forrepl. Also be aware that{}have special meaning within regex and will need to be escaped if you want a literal match.{{.*?}}is a valid pattern. No need to escape anything. It is not Android or C++ (MSVC).