來匹配多個結果。例如,如果我有一個字符串,如「第一個第二個第三個」,並且我想在一個操作中匹配每個單詞以逐個輸出em。如何使用std :: regex
我只是認爲「(\ 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;
}
我期待着您的慈祥的幫助。 :)
這是一個解決方案:regex exp(「(。*)\\ b \\ S * \\ b」); \t smatch res; \t string str =「第一第二第三第四」; \t而(regex_search(STR,RES,EXP,regex_constants :: MATCH_ANY)) \t { \t \t COUT << RES [0] << ENDL; \t \t str = res.suffix()。STR(); \t} – AntiMoron