php 中的正则表达式规则严格性比较

发布于 2024-11-24 04:25:20 字数 115 浏览 2 评论 0原文

有没有办法比较两个或多个正则表达式规则并相应地找出它们的严格性,最好是在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扭转时空 2024-12-01 04:25:20

这是一个不平凡的问题。您需要解析正则表达式并确定每个标记有多少个替代匹配,并且您可能需要递归地执行此操作,以便正确判断嵌套量词如何影响复杂性。

通过查看量词,您可以得到非常粗略的估计,但即使这样也不是微不足道的。

  • ???+?{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 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"...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文