How to use Boost Regex Partial Match

Dear all, Given this code. I expect to get this output from the corresponding input: Input: FOO Output: Match Input: FOOBAR, Output: Match Input: BAR, Output: No Match But why it gives "No Match" for input FOOBAR? __BEGIN__ #include <iostream> #include <vector> #include <fstream> #include <sstream> #include <boost/regex.hpp> using namespace std; using namespace boost; int main ( int arg_count, char *arg_vec[] ) { if (arg_count !=2 ) { cerr << "expected one argument" << endl; return EXIT_FAILURE; } string InputString = arg_vec[1]; string toMatch = "FOO"; const regex e(toMatch); if (regex_match(InputString, e,match_partial)) { cout << "Match" << endl; } else { cout << "No Match" << endl; } return 0; }

Given this code. I expect to get this output from the corresponding input:
Input: FOO Output: Match Input: FOOBAR, Output: Match Input: BAR, Output: No Match
But why it gives "No Match" for input FOOBAR?
You - possibly - have the regex and the string the wrong way around. You are trying to match the regex "FOO" against the string "FOOBAR" and regex_match will only succeed if the *whole of the string matches the regex*, with the flag match_partial set then it will also return true if *the whole of the string matches a prefix of the regular expression*. If you really wanted to match all of the regex against a prefix of the string (a more common situation) then use regex_search with match_continuous set. And of course, if you want to find an occurance of a regex somewhere within the string then set no flags. HTH, John.
participants (2)
-
Gundala Viswanath
-
John Maddock