How to tell if one regular expression matches a subset of another regular expression?

前端 未结 7 2226
孤独总比滥情好
孤独总比滥情好 2020-12-01 14:23

I\'m just wondering if it\'s possible to use one regular expression to match another, that is some sort of:

[\'a-z\'].match([\'b-x\'])
True

[\'m-n\'].match(         


        
相关标签:
7条回答
  • 2020-12-01 15:20

    In addition to antinome's answer:

    Many of the constructs that are not part of the basic regex definition are still regular, and can be converted after parsing the regex (with a real parser, because the language of regex is not regular itself): (x?) to (x|), (x+) to (xx*), character classes like [a-d] to their corresponding union (a|b|c|d) etc. So one can use these constructs and still test whether one regex matches a subset of the other regex using the DFA comparison mentioned by antinome.

    Some constructs, like back references, are not regular, and cannot be represented by NFA or DFA.

    Even the seemingly simple problem of testing whether a regex with back references matches a particular string is NP-complete (http://perl.plover.com/NPC/NPC-3COL.html).

    0 讨论(0)
提交回复
热议问题