关于正则表达式解析器的 C 实现的建议

发布于 2024-10-07 08:17:25 字数 465 浏览 0 评论 0原文

我正在考虑在我正在开发的 C 库中实现正则表达式解析器。现在的问题是:是否有任何开源代码可供我逐字使用或进行尽可能少的更改?我对代码的期望是:

  • 它需要用 C(而不是 C++)编写
  • 它需要在 gcc、mingw、M$VC 下编译
  • 它不能依赖于任何第三方或操作系统特定的头文件/库(即,所有内容)编译所需的内容必须可以通过 gcc、mingw、M$VC 的基本安装轻松获得,
  • 如果它使用 Perl 兼容的正则表达式语法(
  • 理想情况下,代码应该尽可能紧凑,

就像 PHP 中的 PCRE),那就 太好了。有没有您可以推荐的现成解决方案?我正在查看 C 语言的 PCRE,它看起来拥有 PHP 中可用的所有内容(规则),但大小(1.4MB DL)有点吓人。这是一个可靠的选择吗?或者还有其他值得考虑的选择吗?

[编辑]

我正在开发的库是开源的,BSD 许可证。

I'm thinking about implementing a regular expression parser in a C library I'm developing. Now, the question is: is there any open source code that I could use verbatim or with as few changes as possible? My expectations regarding the code are:

  • it needs to be written in C (not C++)
  • it needs to compile under gcc, mingw, M$VC
  • it mustn't depend on any third party or OS-specific headers/libraries (ie, everything needed to compile it must be readily available with a base installation of gcc, mingw, M$VC
  • it would be nice if it used Perl-compatible regex syntax (like PCRE in PHP).
  • ideally, the code should be as compact as possible

Are there any ready-made solutions that you could recommend? I was looking at PCRE for C and it looks like it has everything that's available in PHP (which rules), but the size (1.4MB DL) is a bit intimidating. Do you think it's a solid bet? Or are there other options worth considering?

[EDIT]

The library I'm developing is open source, BSD licence.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

哥,最终变帅啦 2024-10-14 08:17:25

PCRE 之所以如此之大,是因为正则表达式很难。无论如何,其中大部分都是文档和支持代码;当编译成目标代码时它要小得多。

PCRE is so big because regular expressions are hard. And most of it is documentation and support code anyways; it's much smaller when compiled into object code.

吃兔兔 2024-10-14 08:17:25

RE2,Google 正则表达式实现在线性时间内进行匹配 (O(n )(如果 n 是字符串的长度),PCRE 和大多数其他正则表达式引擎在最坏情况下以指数时间运行。另一个值得注意的 O(n) 正则表达式匹配器是 flex,但它需要所有编译时可能的正则表达式。如果您正在寻找比 PCRE 小的东西,请查看 busybox 中的正则表达式匹配器,或中的模式匹配器lua

RE2, the Google regexp implementation does a match in linear time (O(n) if n is the length of the string), PCRE and most other regexp engines run in exponential time at worst case. Another noteworthy O(n) regexp matcher is flex, but it needs all possible regexps at compile time. If you are looking for something smaller than PCRE, look at the regexp matcher in busybox, or the pattern matcher in lua.

断舍离 2024-10-14 08:17:25

如果您对 POSIX 正则表达式语法感到满意,您可以尝试 TRE。如果您想要 Perl 语法,Google 有一个值得一试的新实现。

You might try TRE if you're happy with POSIX regex syntax. If you want Perl syntax, Google has a new implementation worth checking out.

a√萤火虫的光℡ 2024-10-14 08:17:25

PCRE 几乎是正则表达式实现的事实上的标准(有充分的理由)。不用担心大小,它很大,因为正则表达式的实现很复杂。无论如何,就用它吧。

PCRE is pretty much the de facto standard of regex implementations (for a good reason). Don't worry about the size, it's big because regex implementations are complicated. Just use it anyway.

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