例如,如果我有一個字符串,例如“ first second second third”,并且我想在一個操作中匹配每個單詞以一個一個地輸出它們。我只是認(rèn)為“(\ b \ S * \ b){0,}”可以工作。但是實(shí)際上沒有。我該怎么辦?這是我的代碼:#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;} 期待您的幫助。:)
3 回答

慕運(yùn)維8079593
TA貢獻(xiàn)1876條經(jīng)驗 獲得超5個贊
您可以使用suffix()函數(shù),然后再次搜索直到找不到匹配項:
int main()
{
regex exp("(\\b\\S*\\b)");
smatch res;
string str = "first second third forth";
while (regex_search(str, res, exp)) {
cout << res[0] << endl;
str = res.suffix();
}
}
- 3 回答
- 0 關(guān)注
- 1555 瀏覽
添加回答
舉報
0/150
提交
取消