tags:

views:

715

answers:

12

Hello,

What is a good and portable regex library for C/C++? (C++ preferably)

+16  A: 

Boost offers two really good regular expression libraries:

  • My first choice is always Boost.RegEx - it's very powerful and probably what you expect. However, this requires building of some of the Boost files.

  • Boost.Xpressive is header-only (i.e. no need for building a lib, just include). Beside string-based regular expressions, it also offers template-based REs; could be a steep learning curve though ;) ..

However, some people argue that Boost is not light-weight due to the cross-dependencies of all the libraries.

Two other choices:

beef2k
The original author of GRETA Eric Niebler developed Boost.Xpressive as well.
Nemanja Trifunovic
Funny, I didn't know that!
beef2k
+1 for Boost.Xpressive. I heard of it but didn't have time to use it. I'll try it though.
Diego Sevilla
+1  A: 

In case of C++ I would look at Boost Regex

stefanB
+3  A: 

The GNU C library has strong regex support, under the GPL license.

Adam Rosenfield
+4  A: 

I have used the PCRE (Perl Compatible Regular Expressions) library in the past with good success. Its interface isn't quite as nice as Boost RegEx, but it may have fewer overall dependencies.

Greg Hewgill
+1, I use PCRE and have been quite pleased by it.
Soo Wei Tan
+1  A: 

C: Glib.
C++: Boost.

Bastien Léonard
+4  A: 

Boost.RegEx is fine. If you use an up to date C++ compiler providing TR1 libraries then you already have a standard regex library available!

Klaim
+3  A: 

PCRE. There's also PCRE++, which is very easy to use.

c4757p
A: 

Boost is great, but if you do consider it heavyweight it is possible to extract out just the regex part and compile that as separate source files, less than 20 of them.

More information can be found on John Maddock's web pages

David Sykes
+1  A: 

There are really two answers to this question, depending on whether the regular expressions are static or if they're dynamic (input from user or something like that).

If they're static, I recommend Ragel. It's super fast and it turns into inline code. You basically take a C file with Ragel in it, then turn it into a C file that doesn't even use an external libraries. The resulting code is small, and Ragel's regular expression syntax is ridiculously powerful compared to others. For example, if you want to match a string of numbers that isn't "123", you can just type

[0-9]+ - "123"

If you want to match a C-style comment, try:

"/*" any* :>> "*/"

Otherwise go ahead and use PCRE.

Dietrich Epp
A: 

I have had great success with Oniguruma particularly if you need international character support.

The API is very straight forward and you can choose the regex syntax you prefer.

I think Oniguruma is the regex engine used by Ruby too, should be well tested!
Simon Steele
+1  A: 

We used tiny-rex and were very pleased. It is mainly C, but it includes a C++ wrapper with a very small footprint. You can even include it as part of your C or C++ files.

Diego Sevilla
+1 for an interesting library.
A: 

Mine! But you have to download it as part of another tool - csvfix. The regex code is in the alib library, is based on the code in Software Tools In Pascal, ported with a lot of changes to C++. It is very small, about 30K or less of source, supports ed-style rexexes, compiles on Windows and Linux, and I would love to get others to improve it.

anon