Generating a list of values a regex COULD match in Python

后端 未结 4 800
借酒劲吻你
借酒劲吻你 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:30

    The set of matching strings is infinite if and only if there is a quantifier (+ or *) in your regexp. Your question doesn't seem to aim at those patterns. I rather believe that the product function from itertools might help here.

    You might for instance introduce a special character indicating an arbitrary letter (e.g. an underscore), then build a pattern like this

    patt = 'a_c'
    

    and define your alphabet

    youralphabet = 'abcde...'
    

    and define a function generating all possible instances like this

    def genInstances(patt):
        elems = [c if c != '_' else youralphabet for c in patt]
        return itertools.product(*elems)
    

    You may then extend this approach to match real regexp by parsing your pattern for \d or [a-zA-Z] or whatever.

提交回复
热议问题