如果您比對的表達式沒有任何“進階”特性,比如look-ahead or look-behind,那麼您可以自己解析它并建構一個适當的生成器
将regex的每個部分視為傳回某物的函數(例如,1到100個數字之間),并在頂部将它們粘在一起:import random
from string import digits, uppercase, letters
def joiner(*items):
# actually should return lambda as the other functions
return ''.join(item() for item in items)
def roll(item, n1, n2=None):
n2 = n2 or n1
return lambda: ''.join(item() for _ in xrange(random.randint(n1, n2)))
def rand(collection):
return lambda: random.choice(collection)
# this is a generator for /\d{1,10}:[A-Z]{5}/
print joiner(roll(rand(digits), 1, 10),
rand(':'),
roll(rand(uppercase), 5))
# [A-C]{2}\d{2,20}@\w{10,1000}
print joiner(roll(rand('ABC'), 2),
roll(rand(digits), 2, 20),
rand('@'),
roll(rand(letters), 10, 1000))
解析正規表達式是另一個問題。是以這個解決方案不是萬能的,但也許已經足夠了