views:

10872

answers:

7

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.)

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:

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

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

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

Thanks.

+7  A: 

In C++ projects past, I have used PCRE with 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.

Greg Hewgill
+6  A: 

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.

Kasprzol
What compiler has TR1? My copy of g++ 4.1.2 (Debian Etch) does not have support for #include <regex> but thanks for bringing TR1 to my attention, I had forgotten.For others curious to know more on TR1 and C++0x, see http://en.wikipedia.org/wiki/Technical_Report_1
Stéphane
As of SP1 Visual Studio 2008 has most of TR1, including regex. I know it doesn't help you on Linux, but others may be interested. Dinkumware also supports TR1 on gcc.
Michael Burr
As I wrote, if your std library doesn't have regex, then you can use boost: http://www.boost.org/doc/libs/1_36_0/doc/html/boost_tr1/subject_list.html#boost_tr1.subject_list.regex
Kasprzol
+7  A: 

Boost has regex in it.

That should fill the bill

Robert Gould
+3  A: 

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.

Roel
GRETA (http://research.microsoft.com/en-us/downloads/bd99f343-4ff4-4041-8293-34c054efe749/default.aspx) was made by Eric Niebler when he worked at Microsoft (1998-2001 from GRETA's header files). Eric Niebler then made in 2007 Boost.Xpressive. People should use Boost.Xpressive because it's newer and has a nicer license than "Microsoft Research end user license agreement"
Cristian Adam
+23  A: 

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

Personally, I find Boost.Xpressive much 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).

Ferruccio
+3  A: 

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.

Stéphane
A: 

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