Hello. How do I modify this pattern so that regex_search would find ONLY "\w{4}"?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int main()
{
string s;
getline(cin, s);
regex pat(R"(\W\w{4}\W)");
smatch m;
while (regex_search(s, m, pat))
{
for (auto x : m) cout << x << endl;
s = m.suffix().str();
}
system("Pause");
return 0;
}
So... Change the regex to what you want? Remove the \W sets around the regex search term you expect to search for? I'm afraid I'm not sure what library you're using, but generally if you want to search for something you should... search for it..
#include <iostream>
#include <string>
#include <regex>
#include <algorithm>
#include <iterator>
int main()
{
// http://www.regular-expressions.info/wordboundaries.htmlconst std::regex re( R"(\b\w{4}\b)" ) ;
const std::string str = "want to find every four letter word. And this must be the pattern for that." ;
// http://en.cppreference.com/w/cpp/regex/regex_token_iterator
std::copy( std::sregex_token_iterator( str.begin(), str.end(), re ), std::sregex_token_iterator(),
std::ostream_iterator<std::string>( std::cout, "\n" ) ) ;
}