php 中的正则表达式规则严格性比较
有没有办法比较两个或多个正则表达式规则并相应地找出它们的严格性,最好是在 php 中,但如果这存在于不同的平台/语言中,我想知道如何实现它的概念,并相应地在 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
Prithwiraj
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个不平凡的问题。您需要解析正则表达式并确定每个标记有多少个替代匹配,并且您可能需要递归地执行此操作,以便正确判断嵌套量词如何影响复杂性。
通过查看量词,您可以得到非常粗略的估计,但即使这样也不是微不足道的。
??
、?+
、?
、{0,1}?
、{0,1 }+
和{0,1}
各允许一个替代|
,按同一嵌套级别计算,各允许 n+1 个替代{ n,m}?
、{n,m}+
和{n,m}
每个都允许mn
替代方案*+
、*?
、*
、++
、+?
、+
、{n,}?
、{n, }+
和{n,}
每个都允许无限的替代方案还有许多其他的决定复杂性/“严格性”的因素,因此通过此方法您将得到的或多或少是“零近似值”......
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 alternativesAnd there are many other factors that determine complexity/"strictness", so all you'll get by this method is more or less a "zeroth approximation"...