开发者

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 allow m-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"...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜