我一直在几个服务器上使用标准的 UNIX 版本的正则表达式,这些服务器必须对错误具有很强的弹性。多年来,我发现了几个案例,其中要么是用户输入了格式错误的正则表达式,要么是目标字符串中的一些奇怪字符导致了分段违规等,从而导致服务器崩溃。对于我们的系统,这是 Not Acceptable 。
是否有正则表达式版本可以解决这些格式错误的模式或匹配字符串只会导致错误而不会导致服务器崩溃?
由于在检查这些模式时这段代码主要是多线程的,所以我不想使用 setjmp longjmp 等。
最佳答案
有 C++ 正则表达式库 RE2
最初由 Google 为 Go 语言开发,但现在有大多数脚本语言的包装器,如 Perl、Python,它声称:
RE2 uses automata theory to guarantee that regular expression searches run in time linear in the size of the input. RE2 implements memory limits, so that searches can be constrained to a fixed amount of memory. RE2 is engineered to use a small fixed C++ stack footprint no matter what inputs or regular expressions it must process; thus RE2 is useful in multithreaded environments where thread stacks cannot grow arbitrarily large.
因此,似乎可以任选三者:快速、可靠、小巧。
关于c++ - 防撞正则表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13719291/