Regex Rule Strictness comparison in php
Is there a way to compare two or more regex rules and find out their strictness accordingly, iIdeally in php but if this exists in different platform/language I would like to know the concept of how this can be implemented and accordingly code it in php
Thanks P开发者_开发问答rithwiraj
This is a nontrivial problem. You'd need to parse the regex and determine how many alternative matches each token has, and you'd probably need to do this recursively in order to correctly judge how nested quantifiers affect complexity.
You could get a very crude estimate by looking at the quantifiers, but even this is not trivial.
??
,?+
,?
,{0,1}?
,{0,1}+
and{0,1}
each allow one alternative|
, counted at the same level of nesting, each allow n+1 alternatives{n,m}?
,{n,m}+
and{n,m}
each allowm-n
alternatives*+
,*?
,*
,++
,+?
,+
,{n,}?
,{n,}+
and{n,}
each allow infinite alternatives
And there are many other factors that determine complexity/"strictness", so all you'll get by this method is more or less a "zeroth approximation"...
精彩评论