[C++]regex正则匹配

Cplusplus
回复
头像
vicyang
版主
版主
帖子: 56
注册时间: 2016年07月21日 20:35
联系:

[C++]regex正则匹配

帖子 vicyang »

1楼索引
头像
vicyang
版主
版主
帖子: 56
注册时间: 2016年07月21日 20:35
联系:

Re: [C++]regex正则匹配

帖子 vicyang »

遍历输出匹配的子元素
//regex_constants - STL pdf P726
#include <iostream>
#include <string>
#include <regex>

using namespace std;
int main(int argc, char *argv[] )
{
string str("foo<1>bar<2>test\nnewline");

smatch m;
bool found;
found = regex_search(str, m, regex("(\\d).*(\\d)", regex_constants::ECMAScript) );
if (found)
for (auto x:m) std::cout << x << endl;

cout << endl;
found = regex_search(str, m, regex("(\\w+)<\\d>(\\w+)", regex_constants::ECMAScript) );
if (found)
for (auto x:m) std::cout << x << endl;
return 0;
}
m[0] 类似 perl 中的 $&
m[1] m[2] 类似 perl 中的 $1 $2

代码: 全选

1>bar<2
1
2

foo<1>bar
foo
bar
回复

在线用户

正浏览此版面之用户: 没有注册用户 和 1 访客