Hello,
What is a good and portable regex library for C/C++? (C++ preferably)
Hello,
What is a good and portable regex library for C/C++? (C++ preferably)
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:
The GNU C library has strong regex support, under the GPL license.
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.
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!
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
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.
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.
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.
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.