instance
instance

Reputation: 1374

C++ Boost usage over a string

I have no idea about boost, could anybody please tell me what exactly this function is doing?

int
Function(const string& tempStr)
{
    boost::regex expression ("result = ");
    std::string::const_iterator start, end;
    start = tempStr.begin();
    end   = tempStr.end();
    boost::match_results<std::string::const_iterator> what;
    boost::regex_constants::_match_flags flags = boost::match_default;
    int count = 0;
    while(regex_search(start, end, what, expression, flags)){
        start = what[0].second;
        count++;
    }
    cout << "Count :"<< count << endl;
    return count;
}

Upvotes: 2

Views: 177

Answers (4)

Shakiba Moshiri
Shakiba Moshiri

Reputation: 23914

This is a match counter function:

The author uses useless code: here is the equivalent code in std ( also boost )

unsigned int count_match( std::string user_string, const std::string& user_pattern ){

    const std::regex rx( user_pattern );

    std::regex_token_iterator< std::string::const_iterator > first( user_string. begin(), user_string.end(), rx  ), last;

    return std::distance( first, last );

}  

and with std::regex_search it can be (also boost ):

unsigned int match_count( std::string user_string, const std::string& user_pattern ){
    unsigned int counter = 0;
    std::match_results< std::string::const_iterator > match_result;

    std::regex regex( user_pattern );

    while( std::regex_search( user_string, match_result, regex ) ){
        user_string = match_result.suffix().str();
        ++counter;
    }

    return counter;
}

NOTE:
no need to use this part:

std::string::const_iterator start, end;
start = tempStr.begin();
end   = tempStr.end();

Also

boost::match_results<std::string::const_iterator> what;

can be

boost::smatch what // a typedef of match_results<std::string::const_iterator> 

no need:

boost::regex_constants::_match_flags flags = boost::match_default;

because by default regex_search has this flag

this:

start = what[0].second;

is for updating the iteration that can be:

match_result.suffix().str();   

if you want to see what happen in the while loop use this code:

std::cout << "prefix: '" << what.prefix().str() << '\n';
std::cout << "match : '" << what.str() << '\n';
std::cout << "suffix: '" << what.suffix().str() << '\n';
std::cout << "------------------------------\n";

Upvotes: 0

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136505

This function is a complicated way to count the number of occurrences of "result = " string. A simpler way would be:

boost::regex search_string("result = ");
auto begin = boost::make_regex_iterator(tempStr, search_string);
int count = std::distance(begin, {});

Which can be collapsed to a one-liner, with possible loss of readability.

Upvotes: 0

Marco A.
Marco A.

Reputation: 43662

match_results is a collection of sub_match objects. The first sub_match object (index 0) represents the full match in the target sequence (subsequent matches would correspond to the subexpressions matches). Your code is searching for result = matches and restarting the search each time from the end of the previous match (what[0].second)

int
Function(const string& tempStr)
{
    boost::regex expression ("result = ");
    std::string::const_iterator start, end;
    start = tempStr.begin();
    end   = tempStr.end();
    boost::match_results<std::string::const_iterator> what;
    boost::regex_constants::_match_flags flags = boost::match_default;
    int count = 0;
    while(regex_search(start, end, what, expression, flags)){
        start = what[0].second;
        count++;
    }
    cout << "Count :"<< count << endl;
    return count;
}

int main()
{
    Function("result = 22, result = 33"); // Outputs 'Count: 2'
}

Live Example

Upvotes: 2

orip
orip

Reputation: 75537

The base of the functionality is searching for a regular expression match on tempStr.

Look at the regex_search documentation and notice what the match_result contains after it finishes (that's the 3rd parameter, or what in your code sample). From there understanding the while loop should be straightforward.

Upvotes: 0

Related Questions