Re: [Boost-users] [Regex] Accessing match count
The regexes in my previous post may be borked due to email client stripping stuff out. Let's just assume that regex_consonant and regex_vowel are well formed patterns. The code below seems to be getting closer. It returns a plausible count for number of matches of regex 'expression' in string 'word': string get_m(string word) { regex c("^" + regex_consonant); regex v(regex_vowel + "$"); string re = "(" + regex_vowel + "+" + regex_consonant + "+)"; regex expression(re); string replacement = ""; word = regex_replace(word, c, replacement); word = regex_replace(word, v, replacement); boost::sregex_token_iterator itr(word.begin(), word.end(), expression); boost::sregex_token_iterator end; int size = 0; for(; itr != end; ++itr) { size++; } string s; stringstream out; out << size; s = out.str(); return s; } Is boost::sregex_token_iterator the appropriate function for subsequently counting the number of matches found in a given string? Also, don't hold back on how bad this code is, I'm here to learn! Mick
ninti@internode.on.net" wrote:
The regexes in my previous post may be borked due to email client stripping stuff out. Let's just assume that regex_consonant and regex_vowel are well formed patterns.
The code below seems to be getting closer. It returns a plausible count for number of matches of regex 'expression' in string 'word':
string get_m(string word) { regex c("^" + regex_consonant); regex v(regex_vowel + "$"); string re = "(" + regex_vowel + "+" + regex_consonant + "+)"; regex expression(re);
Does replacing the remainder of this function with: return boost::lexical_cast< std::string >( std::distance( boost::sregex_iterator( word.begin(), word.end(), expression ), boost::sregex_iterator() ) ); help?
string replacement = ""; word = regex_replace(word, c, replacement); word = regex_replace(word, v, replacement);
boost::sregex_token_iterator itr(word.begin(), word.end(), expression); boost::sregex_token_iterator end;
int size = 0; for(; itr != end; ++itr) { size++; }
string s; stringstream out; out << size; s = out.str();
return s; }
Is boost::sregex_token_iterator the appropriate function for subsequently counting the number of matches found in a given string?
Also, don't hold back on how bad this code is, I'm here to learn!
Mick
Best Regards, Gevorg
participants (2)
-
Gevorg Voskanyan
-
nintiļ¼ internode.on.net