Linux C++:我应该使用什么正则表达式库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/181624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:34:37  来源:igfitidea点击:

C++: what regex library should I use?

c++regexlinux

提问by Stéphane

I'm working on a commercial (not open source) C++ project that runs on a linux-based system. I need to do some regex within the C++ code. (I know: I now have 2 problems.)

我正在开发一个在基于 linux 的系统上运行的商业(非开源)C++ 项目。我需要在 C++ 代码中做一些正则表达式。(我知道:我现在有两个问题。)

QUESTION: What libraries do people who regularly do regex from C/C++ recommend I look into? A quick search has brought the following to my attention:

问题:经常使用 C/C++ 进行正则表达式的人推荐我查看哪些库?快速搜索引起了我的注意:

1) Boost.Regex (I need to go read the Boost Software License, but this question is not about software licenses)

1)Boost.Regex(我需要阅读Boost软件许可证,但这个问题与软件许可证无关)

2) C (not C++) POSIX regex (#include <regex.h>, regcomp, regexec, etc.)

2) C(非 C++)POSIX 正则表达式(#include <regex.h>、regcomp、regexec 等)

3) http://freshmeat.net/projects/cpp_regex/(I know nothing about this one; seems to be GPL, therefore not usable on this project)

3)http://freshmeat.net/projects/cpp_regex/(我对这个一无所知;似乎是 GPL,因此不能用于这个项目)

采纳答案by Stéphane

Thanks for all the suggestions.

感谢所有的建议。

I tried out a few things today, and with the stuff we're trying to do, I opted for the simplest solution where I don't have to download any other 3rd-party library. In the end, I #include <regex.h> and used the standard C POSIX calls regcomp() and regexec(). Not C++, but in a pinch this proved to be the easiest.

我今天尝试了一些东西,对于我们正在尝试做的事情,我选择了最简单的解决方案,我不必下载任何其他 3rd 方库。最后,我 #include <regex.h> 并使用标准 C POSIX 调用 regcomp() 和 regexec()。不是 C++,但在紧要关头,这被证明是最简单的。

回答by Greg Hewgill

In C++ projects past, I have used PCREwith good success. It's very complete and well-tested since it's used in many high profile projects. And I see that Google has contributed a set of C++ wrappers for PCRE recently, too.

在过去的 C++ 项目中,我使用PCRE并取得了成功。它非常完整且经过充分测试,因为它已用于许多备受瞩目的项目。而且我看到 Google 最近也为 PCRE 贡献了一组 C++ 包装器。

回答by Kasprzol

C++ has a builtin regex library since TR1. AFAIK Boost's regex library is very compatible with it and can be used as a replacement, if your standard library doesn't provide TR1.

从 TR1 开始,C++ 就有一个内置的正则表达式库。AFAIK Boost 的正则表达式库与它非常兼容,如果您的标准库不提供 TR1,则可以用作替代品。

回答by Robert Gould

Boost has regex in it.

Boost 中有正则表达式

That should fill the bill

那应该填帐单

回答by Roel

I've personally always used boost.regex (although I don't have much need for regex in C++). Microsoft Labs has a regex library too, called GRETA: http://research.microsoft.com/projects/greta/. Apparently it's very fast and features a whole Perl 5 syntax. I haven't used it, but you may want to test it out.

我个人一直使用 boost.regex (虽然我对 C++ 中的 regex 没有太多需求)。Microsoft Labs 也有一个正则表达式库,称为 GRETA:http: //research.microsoft.com/projects/greta/。显然它非常快并且具有完整的 Perl 5 语法。我没用过,但你可能想测试一下。

回答by Ferruccio

Boost.Regexis very good and is slated to become part of the C++0x standard (it's already in TR1).

Boost.Regex非常好,并计划成为 C++0x 标准的一部分(它已经在 TR1 中)。

Personally, I find Boost.Xpressivemuch nicer to work with. It is a header-only library and it has some nice features such as static regexes (regexes compiled at compile time).

就个人而言,我发现Boost.Xpressive更适合使用。它是一个仅包含头文件的库,并且具有一些不错的功能,例如静态正则表达式(在编译时编译的正则表达式)。

Update:If you're using a C++11 compliant compiler (gcc 4.8 is NOT!), use std::regexunless you have good reason to use something else.

更新:如果您使用的是 C++11 兼容编译器(gcc 4.8 不是!),除非您有充分的理由使用其他东西,否则请使用std::regex

回答by MartinKahn

I faced a similar situation and ended up using Henry Spencers Regexp Engine http://www.codeproject.com/KB/string/spencerregexp.aspx

我遇到了类似的情况,最终使用了 Henry Spencers Regexp Engine http://www.codeproject.com/KB/string/spencerregexp.aspx

回答by Alexander Gololobov

You can also look at fast regex librarythat was developed at Yandexsearch engine for doing fast matches of thousands of patterns against huge amounts of data.

您还可以查看在Yandex搜索引擎开发的快速正则表达式库,用于对大量数据进行数千种模式的快速匹配。

回答by RedX

Noone here said anything about the one that comes with C++0x. If you are using a compiler and the STL that supports C++0x you could just use that instead of having another lib in your project.

这里没有人提到 C++0x 附带的那个。如果您使用的是支持 C++0x 的编译器和 STL,您可以直接使用它,而不是在您的项目中使用另一个库。

回答by matiu

Two more options:

还有两个选择:

If you can write it in c++11- Do the tutorial: http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c15339

如果你可以用c++11写它- 做教程:http: //www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c15339

Note: At the time of writing the only c++11 regex library that I know works is the clang/llvm one, and only works on Mac. The GNU still doesn't implement regex yet. I don't know about Visual Studio. Most people still use the boost regeximplementation.

注意:在撰写本文时,我知道唯一可用的c++11 正则表达式库是clang/llvm one,并且仅适用于 Mac。GNU 仍然没有实现正则表达式。我不知道 Visual Studio。大多数人仍然使用boost regex实现。



Or you can use ragelto generate a finite state machine to do the parsing for you, and generate the C/C++ code implementation: http://www.complang.org/ragel/

或者你可以使用ragel生成一个有限状态机来帮你解析,生成 C/C++ 代码实现:http: //www.complang.org/ragel/

I used it a little to generate code to parse json. This ragel file: https://github.com/matiu2/yajp/blob/master/parser/number.rlis used to generate this code https://github.com/matiu2/yajp/blob/master/parser/json.hpp#L254and this finite state machine diagram:

我稍微用了一下,生成了解析json的代码。这个 ragel 文件:https: //github.com/matiu2/yajp/blob/master/parser/number.rl用于生成此代码 https://github.com/matiu2/yajp/blob/master/parser/json .hpp#L254和这个有限状态机图:

state diagram

状态图



Update 1:

更新 1:

lvm's libc++ regex works on ubuntu 14.04: libc++-dev - LLVM C++ Standard library (development files). When compiling: clang++ -std=c++11 -lc++ -I/usr/include/c++/v1 ...

lvm 的 libc++ 正则表达式适用于 ubuntu 14.04:libc++-dev - LLVM C++ 标准库(开发文件)。编译时:clang++ -std=c++11 -lc++ -I/usr/include/c++/v1 ...

Update 2:

更新 2:

I'm currently enjoying boost spirit 3- I like it more than regex, because it has BNF style rules and is well thought out. (Older (more documented) Spirit Qi libs found here)

我目前正在享受boost Spirit 3- 我比正则表达式更喜欢它,因为它具有 BNF 风格的规则并且经过深思熟虑。(旧的(更多记录的)Spirit Qi 库在这里找到