tags:

views:

528

answers:

10

Duplicate of: There is a function to use pattern matching (using regular expressions) in C++?

I'm not sure where one would use it... are there any parser-type functions that take some regex as an argument or something? I just found out that my editor will highlight a line after / as "regex" for C/C++ syntax which I thought was weird...

+18  A: 

In the vanilla C++ language there is no support for regular expressions. However there are several libraries available that support Regex's. Boost is a popular one.

Check out Boost's Regex implementation.

JaredPar
Until TR1 is supported by compilers.
sharth
+8  A: 

PCRE is the de-facto standard regex library for C (and it also works in C++).

(What your editor is doing I don't know. Using a library like PCRE or any of the others suggested doesn't change the syntax of C - your regex definitions will always be held in strings.)

RichieHindle
A: 

I don't think you can perform a regex in C++ without using some third-party library. Qt and Gtk+/Gtkmm both come with these. Visual C++ 2008 also comes with this ability.

Lucas McCoy
+1  A: 

Boost.Xpressive allows you to write regexs as strings (like in Boost.Regex) or statically with expression templates. It is similar to Boost.Spirit for grammars.

For example, these two are equivalent:

sregex rex1 = sregex::compile("(\\w+) (\\w+)!"); //normal string based way
sregex rex2 = (s1= +_w) >> ' ' >> (s2= +_w) >> '!'; //expression template way
Zifre
+2  A: 

Regular Expressions are part of the C++ standard library extension defined in TR1 (see Chapter 7 in Documentation). The dinkumware library i.e has implemented the regEx extensions. I dont know about other implementations.

The extensions are simple and straight forward to use.

RED SOFT ADAIR
+1  A: 

Just for completeness, Qt has a QRegExp that can do regular expression matching.
This is usually good if you need a small regexp for a remote feature in your grand Qt application. For anything more serious, PCRE is definitely the way to go.

shoosh
+1  A: 

No, C++ does not have, and is not going to get, regexes using the /.../ syntax used in some languages. Your editor is wrong.

As all the other answers show, regex libraries for C++ do exist (And one is scheduled for inclusion in C++0x), but they process strings, delimited by ", not slashes, so they are not the reason for your editor's behavior.

jalf
A: 

If you are in Visual Studio you can use Greta (search greta regex) but i think it's a bit slower than boost. It's really easy to use though.

Enigme
A: 

If you use visual studio and portability is not a major issue, you can get results pretty quickly (no downloads, no installations) with a cute ATL facility called CAtlRegExp. It contains full and efficient RegEx parsing and matching (online sample). Haven't compared its performance to BOOST, though.

Ofek Shilon