例如,如果我有一个像“first second third Fourth”这样的字符串,并且我想在一个操作中匹配每个单词以逐个输出它们。
我只是认为 "(\\b\\S*\\b){0,}"
会起作用。但实际上并没有。
我应该怎么办?
这是我的代码:
#include<iostream>
#include<string>
using namespace std;
int main()
{
regex exp("(\\b\\S*\\b)");
smatch res;
string str = "first second third forth";
regex_search(str, res, exp);
cout << res[0] <<" "<<res[1]<<" "<<res[2]<<" "<<res[3]<< endl;
}
原文由 AntiMoron 发布,翻译遵循 CC BY-SA 4.0 许可协议
这可以在
regex
的C++11
中完成。两种方法:
()
regex
来定义您的捕获。像这样:
现场观看: http ://coliru.stacked-crooked.com/a/e1447c4cff9ea3e7
sregex_token_iterator()
:现场观看: http ://coliru.stacked-crooked.com/a/677aa6f0bb0612f0