Comparing two regular expressions for strict-ness?
In general, is there a quick way to compare two regular expressions and det开发者_运维百科ermine which of the two is more strict? Ie, with two regexes A and B, A is stricter than B iff every string that matches A also matches B.
While there are algorithms that let you do these kinds of things in theory, applying them to Ruby will be difficult for several reasons:
- Ruby doesn't provide these features as builtins, becuase the goal of Ruby's regular expressions is to match text, not perform theoretical manipulations of finite state automata.
- Ruby doesn't expose the internals of its regular expressions for you to run your own algorithms on. (Though you could use
inspect
to get the original form of the regular expression, you'd have to parse it yourself to perform any analysis of the regular expression.) - Ruby's regular expressions aren't "real" regular expressions. Thanks to backreferences, used in conjunction with capturing groups, they can recognize a superset of the regular languages (though it's only a subset of the context free languages), so the theory wouldn't match up perfectly with the power of Ruby regular expressions.
精彩评论