Generating a list of values a regex COULD match in Python

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

    Some regular expressions match a finite number of input strings, but many (most?) match an infinite number of input strings. It's kind of like asking 'given the python language grammar, generate all possible python programs'. You probably could write a program to list them all in sequence if you tried (though it would take infinite time to run), but are you sure you want to? Why would you want to?

    I'm pretty sure the regular expression engine in the standard library does not expose a way to generate the output you want. You'd have to get lower level access to the internal data structures, or implement some DFA engine thing-a-ma-bob yourself.

提交回复
热议问题