fork download
  1. # your code goes here
  2.  
  3. import sre_parse
  4. import pprint
  5. import random
  6.  
  7.  
  8. class RandomRegexStringGenerator:
  9. def __init__(self):
  10. self._words = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'
  11. self._digits = '0123456789'
  12. self._whitespaces = ' \t' # ' \t\n\r\v\f' 에서 조정
  13. self._max_repeat = 5 # 무한 수량자가 나왔을때 최대 반복 리밋
  14.  
  15. self._result = ''
  16.  
  17. def _gen_char(self, value):
  18. return chr(value)
  19.  
  20. def _gen_range(self, min_value, max_value):
  21. # random 선택
  22. idx = random.randint(min_value, max_value)
  23. return chr(idx)
  24.  
  25. def _gen_in_word(self):
  26. return self._words[random.randint(0, len(self._words)-1)]
  27.  
  28. def _gen_in_digit(self):
  29. return self._digits[random.randint(0, len(self._digits)-1)]
  30.  
  31. def _gen_in_whitespace(self):
  32. return self._whitespaces[random.randint(0, len(self._whitespaces)-1)]
  33.  
  34.  
  35. def _proc_cmd(self, itm):
  36. # 단문자
  37. if itm[0] == sre_parse.LITERAL:
  38. self._result += self._gen_char(itm[1])
  39.  
  40. # 문자클래스
  41. elif itm[0] == sre_parse.IN:
  42. start_num = 0
  43. body = itm[1]
  44. if body[0][0] == sre_parse.NEGATE: # 제외조건 TODO:
  45. start_num = 1
  46.  
  47. # random 선택
  48. idx = random.randint(start_num, len(body)-1)
  49. cls_itm = body[idx]
  50.  
  51. if cls_itm[0] == sre_parse.LITERAL:
  52. self._result += self._gen_char(cls_itm[1])
  53.  
  54. elif cls_itm[0] == sre_parse.RANGE:
  55. self._result += self._gen_range(cls_itm[1][0], cls_itm[1][1])
  56.  
  57. elif cls_itm[0] == sre_parse.CATEGORY:
  58. if cls_itm[1] == sre_parse.CATEGORY_DIGIT:
  59. self._result += self._gen_in_digit()
  60.  
  61. elif cls_itm[1] == sre_parse.CATEGORY_WORD:
  62. self._result += self._gen_in_word()
  63.  
  64. elif cls_itm[1] == sre_parse.CATEGORY_SPACE:
  65. self._result += self._gen_in_whitespace()
  66. # TODO: NOT CATEGORY 처리해야함..
  67.  
  68. # 수량자
  69. elif itm[0] in (sre_parse.MAX_REPEAT, sre_parse.MIN_REPEAT):
  70. # 패턴 => XXX_REPEAT, (min, max, p)
  71. min_repeat = itm[1][0]
  72. max_repeat = itm[1][1]
  73. # set limit
  74. if itm[1][1] == sre_parse.MAXREPEAT:
  75. max_repeat = self._max_repeat # 지정한 max값으로 한정
  76.  
  77. # random 선택
  78. rand_repeat = random.randint(min_repeat, max_repeat)
  79.  
  80. # random 만큼 반복
  81. for i in range(rand_repeat):
  82. self._read_body(itm[1][2])
  83.  
  84. # Union
  85. elif itm[0] == sre_parse.BRANCH:
  86. body = itm[1][1]
  87. # random 선택
  88. idx = random.randint(0, len(body)-1)
  89. self._read_body(body[idx])
  90.  
  91. else:
  92. if isinstance(itm[1], tuple):
  93. for titm in itm[1]:
  94. if isinstance(titm, sre_parse.SubPattern):
  95. #print('CMD: %s in body '%(itm[0]))
  96. self._read_body(titm)
  97.  
  98. # [ (cmd), (cmd), ...] 이런 형태에 대해 cmd 마다 처리
  99. def _read_body(self, body):
  100. for itm in body:
  101. self._proc_cmd(itm)
  102.  
  103.  
  104. def generate(self, regex_pattern):
  105.  
  106. p = sre_parse.parse( regex_pattern )
  107. #pprint.pprint(p)
  108.  
  109. self._result = ''
  110. self._read_body( p )
  111.  
  112. return self._result
  113.  
  114.  
  115.  
  116.  
  117.  
  118. pat = r'01[069]-\d\d\d\d?(-\d\d\d\d){2}'
  119.  
  120. g = RandomRegexStringGenerator()
  121.  
  122. for i in range(10):
  123. rstr = g.generate( pat )
  124. print( rstr )
  125.  
  126.  
Success #stdin #stdout 0.03s 11848KB
stdin
Standard input is empty
stdout
010-631-4279-4705
010-6518-5752-3683
016-124-0882-4711
019-583-3439-1319
016-072-6866-4919
019-4837-2730-3732
019-9947-2561-3228
019-7507-0317-3081
019-563-9371-9878
016-8216-5173-5505