Random data generator mathing a regex in python -


in python, looking python code can use create random data matching regex. example, if regex is

\d{1,100} 

i want have list of random numbers random length between 1 , 100 (equally distributed)

there 'regex inverters' available (see here) compute possible matches, not want, , extremely impracticable. example above, example, has more 10^100 possible matches, never can stored in list. need function return match random.

maybe there package available can used accomplish this? need function creates matching string regex, not given 1 or other, maybe 100 different regex. cannot code them myself, want function extract pattern return me matching string.

if expressions match not have "advanced" features, look-ahead or look-behind, can parse , build proper generator

treat each part of regex function returning something (e.g., between 1 , 100 digits) , glue them @ top:

import random string import digits, uppercase, letters  def joiner(*items):     # should return lambda other functions     return ''.join(item() item in items)    def roll(item, n1, n2=none):     n2 = n2 or n1     return lambda: ''.join(item() _ in xrange(random.randint(n1, n2)))  def rand(collection):     return lambda: random.choice(collection)  # generator /\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)) 

parsing regex question. solution not universal, maybe it's sufficient


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -