Regular expression for countWords()

Hi,

countWords() returns 10 for the string:

".. , ! ? Hello,, ,,,,,,, ,, word..! apple orange!"


1
2
3
4
5
6
7
8
9
10
11
12
int countWords(const std::string& str) {
    std::vector< std::string > result;
    static const boost::regex re("\\s+");
    boost::algorithm::split_regex(result, str, re);
    return result.size();
}

int main(int argc, char** argv) {
    std::string inputStr = ".. , ! ? Hello,, ,,,,,,, ,, word..! apple orange!";
    std::cout << countWords(inputStr) << std::endl;
    return 0;
}


Thank you!
I wrote:

static const boost::regex re("[^0-9a-zA-Z]+");

Now the countWords returns 6 for the string:

".. , ! ? Hello,, ,,,,,,, ,, word..! apple orange!"
Last edited on
I found the solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <vector>
#include <string>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/regex.hpp>

int countWords(const std::string& str) {
    std::vector< std::string > result;
    static const boost::regex re("[A-Za-z0-9]+");
    boost::algorithm::split_regex(result, str, re);
    return result.size()-1;
}

int main(int argc, char** argv) {
    std::string inputStr = ".. , ! ? Hello,, ,,,,,,, ,, word..! apple orange!";
    std::cout << "nw = " << countWords(inputStr) << std::endl;
    return 0;
}


Output:

nw = 4
Last edited on
Topic archived. No new replies allowed.