is it possible to use regex in c++?

2020-07-13 12:09发布

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

标签: c++ c regex
10条回答
何必那么认真
2楼-- · 2020-07-13 12:15

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.

查看更多
做个烂人
3楼-- · 2020-07-13 12:17

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
查看更多
淡お忘
4楼-- · 2020-07-13 12:22

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

查看更多
Melony?
5楼-- · 2020-07-13 12:23

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.

查看更多
姐就是有狂的资本
6楼-- · 2020-07-13 12:27

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.

查看更多
走好不送
7楼-- · 2020-07-13 12:29

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.

查看更多
登录 后发表回答