regex to find each 5-letter word in a sentence

I am using the following data string and regexes to find each 5-letter word in a sentence, but I get incorrect results as indicated:

1
2
3
4
5
6
7
8
9
10
11
    string sentence = "The quick brown fox jumped over the lazy dog,"
                      "and then ran away!";
    
    /// finds 3 words : quick, brown, jumpe
    regex FiveLetterWordsa {"(\\w{5})"};

    /// finds 3 words : quick, brown, umped
    regex FiveLetterWordsc {"(\\w{5}[^\\w]+)"};
    
    /// finds 1 word: quick
    regex FiveLetterWords {"([^\\w]+\\w{5}[^\\w]+)"};


What would be the correct regex to return the expected number of 5-letter words in the above sentence: (which is two)?

Thanks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
#include <regex>
#include <iterator> // std::distance


int main()
{
    std::string s = "The quick brown fox jumped over the lazy dog,"
                      "and then ran away!";

    std::regex five_letter_words("\\b[[:alnum:]]{5}\\b");
    //or [:alpha:]

    auto words_begin =
    std::sregex_iterator(s.begin(), s.end(), five_letter_words);
  auto words_end = std::sregex_iterator();

  std::cout << "Found " << std::distance(words_begin, words_end) << " five-letter words:\n";

  for (std::sregex_iterator i = words_begin; i != words_end; ++i)
  {
    std::cout << (*i).str() << " at position " << (*i).position() << '\n';
  }
}

//http://www.cplusplus.com/reference/regex/ECMAScript/




I'd suggest not using a regex. Just split the string into "words" using your desired criteria and then filter.
I'd suggest not using a regex ...

could you pls clarify why? sometime std::regex does indeed trip on edge cases but are there any dangers in this instance we should be aware of? thanks
gunner's solution works perfectly.

\\b in a regex specifies a word boundary.

http://cpp.sh/8bfu2

Thanks.
Topic archived. No new replies allowed.