Boost logo

Boost Users :

Subject: Re: [Boost-users] [Regex] Accessing match count
From: Gevorg Voskanyan (v_gevorg_at_[hidden])
Date: 2010-03-10 09:23:24


ninti_at_[hidden]" 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


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net