Carson Myers
Carson Myers

Reputation: 38564

is it possible to use regex in c++?

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

Upvotes: 5

Views: 1315

Answers (10)

Ofek Shilon
Ofek Shilon

Reputation: 16129

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.

Upvotes: 1

Enigme
Enigme

Reputation: 31

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.

Upvotes: 0

RichieHindle
RichieHindle

Reputation: 281485

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

Upvotes: 8

Stack Overflow is garbage
Stack Overflow is garbage

Reputation: 247969

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.

Upvotes: 1

shoosh
shoosh

Reputation: 78914

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.

Upvotes: 1

RED SOFT ADAIR
RED SOFT ADAIR

Reputation: 12218

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.

Upvotes: 2

Zifre
Zifre

Reputation: 26998

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

Upvotes: 1

Kredns
Kredns

Reputation: 37201

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.

Upvotes: 0

JaredPar
JaredPar

Reputation: 754725

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.

Upvotes: 18

Related Questions