Generating a list of values a regex COULD match in Python

后端 未结 4 802
借酒劲吻你
借酒劲吻你 2021-01-07 00:04

I\'m trying to use a regex as an input, and from there generate all the possible values that the regex would match.

So, for example, if the regex is \"three-letter w

4条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-07 00:18

    You don't want to do this. Most of the result sets will be huge, and some will be infinite. Instead use a sequence of test vectors and apply the regex against each in turn:

    vectors = (
      'foo',
      'bar',
      ...
    )
    
    for result in (re.match(someregex, entry) for entry in vectors):
      ...
    

提交回复
热议问题