for example.If I have a string like"first second third forth"and I want to match each single word in one operation to output'em one by one.
I just thought that "(\b\S*\b){0,}" would work.But actually it did not.
What should I do?
Here's my code:
#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; }
I'm looking forward to your kindly help. :)
Simply iterate over your string while regex_searching, like this:
{ regex exp("(\\b\\S*\\b)"); smatch res; string str = "first second third forth"; string::const_iterator searchStart( str.cbegin() ); while ( regex_search( searchStart, str.cend(), res, exp ) ) { cout << ( searchStart == str.cbegin() ? "" : " " ) << res[0]; searchStart = res.suffix().first; } cout << endl; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With