Try re.sub
with a replacing function:
import re, random
def rand_replace(m):
return random.choice(m.group(1).split('|'))
data = '''Random digits: (1|2|3), (4|5|6), (7|8|9).'''
print re.sub(r'\((.+?)\)', rand_replace, data)
This also works for non-digit strings, like "(nice|good) (day|night)". If you want to be specific, this regexp:
r'\(([\d|]+)\)'
accepts only digits and pipes.