如何验证正则表达式模式是否为常量模式? [英] How to validate whether a regular expression pattern is a constant pattern?

查看:35
本文介绍了如何验证正则表达式模式是否为常量模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个正则表达式

Assuming I have a regular expression

^foobar$

我可以放心地假设它是常量(它只会匹配 foobar 而不会匹配其他任何东西).

I can safely assume it is constant (it will only ever match foobar and nothing else).

现在假设我有以下内容

^foo.ar$

现在它不是常量,因为 . 可以匹配任何字符.

Now it is not constant, as the . can match any character.

是否有任何标准方法可以检查正则表达式模式(在我的情况下为 PCRE)是否恒定?我是否应该天真地为诸如 (){}[]?.+* 之类的没有反斜杠前缀的常见字符正则表达式"?

Is there any standard method to check if a regular expression pattern (PCRE in my case) constant or not? Should I just naively "regex the regex" for the common characters such as (){}[]?.+* that are not prefixed with a backslash?

我正在编写一个静态分析工具,它应该建议在使用常量模式时用更简单的基于字符串的函数替换某些 PCRE 驱动的函数.

I'm writing a static analysis tool that should suggest replacing certain PCRE powered functions with simpler string based functions when constant patterns are in use.

推荐答案

考虑使用现有的正则表达式解析器,该解析器输出 AST.

Consider using an existing Regular Expression parser which outputs an AST.

以 JavaScript 为例:
https://www.npmjs.com/package/regjsparser
https://github.com/jviereck/regjsparser

For example for JavaScript:
https://www.npmjs.com/package/regjsparser
https://github.com/jviereck/regjsparser

此处的演示页面可让您查看生成的 AST:
http://www.julianviereck.de/regjsparser/

The demo page here allows you to see the generated AST:
http://www.julianviereck.de/regjsparser/

对于^foobar$:
http://www.julianviereck.de/regjsparser/#%2F%5Efoobar%24%2Fiu

对于^foo.ar$:
http://www.julianviereck.de/regjsparser/#%2F%5Efoo.ar%24%2Fiu

然后您可以在 AST 中搜索类型",在这种情况下,第二个示例包括点"类型:

Then you could search for the "type" in the AST, in this case the 2nd example includes the "dot" type:

    {
      "type": "dot",
      "range": [
        4,
        5
      ],
      "raw": "."
    },

您还可以检查characterClass"和其他人 - 或者反过来可能只需要一个允许列表".

You might also check for "characterClass" and others - or the reverse might be advisable to only have an "allowed list".

另请注意,有一个 JS 库可以从 AST 生成正则表达式:
https://www.npmjs.com/package/regjsgen
https://github.com/bnjmnt4n/regjsgen

Also note there is a JS library to generate regular expressions from the AST:
https://www.npmjs.com/package/regjsgen
https://github.com/bnjmnt4n/regjsgen

这篇关于如何验证正则表达式模式是否为常量模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆