Boost logo

Boost Users :

Subject: [Boost-users] REGEX Don't understand the matching behavior: regex_search vs. regex_match
From: Andy Falanga (afalanga) (afalanga_at_[hidden])
Date: 2012-02-22 11:37:27


Hi,

I understand enough that regex_match() searches for matches based on the entire input string and apparently regex_search() allows for matches of the expression within the input string (i.e. the expression need not match the entire input string). However, I'm not understanding why the following happened:

const char* pInput("Loc info: Channel 2, Target 0, Lun 0");
boost::cmatch found;
boost::regex expression(".*Channel ([[:digit:]]{1,3})); // looking for the channel number only
if(boost::regex_match(pInput, found, expression, boost::regex_constants::match_partial)) {
    std::cout << "Matched: " << std::atoi(found[1].first) << std::endl;
}

When using the above, with regex_match(), the match succeeded but the output string was: "Matched: 0". What?!? If the expression was matched, why did the capture group 1 contain 0? How should the expression have been made?

That's what I don't understand. I've found that what I want is accomplished with altering the above thusly:

const char* pInput("Loc info: Channel 2, Target 0, Lun 0");
boost::cmatch found;
boost::regex expression("Channel ([[:digit:]]{1,3})); // looking for the channel number only
if(boost::regex_search(pInput, found, expression)) {
    std::cout << "Matched: " << std::atoi(found[1].first) << std::endl;
}

This gives me: "Matched: 2." Which is what I'm looking for. I'd like to understand why regex_match() wasn't giving me what I wanted.

Thanks everyone,
Andy



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