REGEX Don't understand the matching behavior: regex_search vs. regex_match
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
Thank you, norulez, that worked.
Andy
From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of norulez@me.com
Sent: Wednesday, February 22, 2012 10:01 AM
To: boost-users@lists.boost.org
Cc: boost-users@lists.boost.org
Subject: Re: [Boost-users] REGEX Don't understand the matching behavior: regex_search vs. regex_match
Try it with the following:
boost::regex expression("Channel ([[:digit:]]{1,3}).*");
Note the .* here
Best Regards
Am 22.02.2012 um 17:37 schrieb "Andy Falanga (afalanga)"
participants (2)
-
Andy Falanga (afalanga)
-
norulez@me.com