boost::match_results not empty but cannot access

I'm using boost::regex to capture specific format strings from a html file. Here is a piece of my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
for( unsigned i = 0; i != resNum; ++i ) {
        stringstream temp;
        temp << i+1;
        string idx;
        temp >> idx;
        string pstr("<div class=\"result(-op)?\\s\\bc-container.+\\bid=\""+idx+"\"");
        cout << pstr << endl;
        regex pattern2find(pstr, regex::ECMAScript | regex::icase);
        smatch matchRes;
        regex_search(web_all, matchRes, pattern2find, regex_constants::match_default);
        if( matchRes.empty() ){
            break;
        }else{
            startPos.push_back(matchRes.position());
            cout << matchRes.str() << endl; //this works fine
        }
    }

    //get the title and abstract of the search results
    for( unsigned i = 0; i != startPos.size(); ++i ){
        string pstr_t("<h3 class=\"t\">[^\\w]*<a[^\\}]*[^\\w]*href[^>]*>.*</a>");
        string pstr_a("<div class=\"c-abstract[^>]*>.*</div>");
        cout << pstr_t << endl << pstr_a << endl;
        regex titleP2F(pstr_t, regex::ECMAScript | regex::icase);
        regex abstP2F(pstr_a, regex::ECMAScript | regex::icase);
		
        boost::smatch titleMRes;
        boost::smatch abstMRes;
	if(i != startPos.size()-1){
            regex_search<string::const_iterator>(web_all.begin()+startPos[i], web_all.begin()+startPos[i+1], titleMRes, titleP2F, regex_constants::match_default);
            regex_search<string::const_iterator>(web_all.begin()+startPos[i], web_all.begin()+startPos[i+1], abstMRes, abstP2F, regex_constants::match_default);
	}else{
            regex_search<string::const_iterator>(web_all.begin()+startPos[i], web_all.end(), titleMRes, titleP2F, regex_constants::match_default);
            regex_search<string::const_iterator>(web_all.begin()+startPos[i], web_all.end(), abstMRes, abstP2F, regex_constants::match_default);
	}
		
        if( !titleMRes.empty() ){
            cout << titleMRes.str() << endl; // error here
        }
        if( !abstMRes.empty() ){
            cout << abstMRes.str() << endl;
        }
    }


And the error is:
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<std::logic_error> >'
  what():  Attempt to access an uninitialzed boost::match_results<> class.
Aborted (core dumped)

How come the first one works but the last two cannot? And the match_results is not empty and should be ready after calling regex_search!

PS: my environment: g++4.8.2, boost1.55.0
Ok, I found the reason. It seems that boost doesn't behave exactly the same with what the c++ standard says. I should check the Boolean returned by regex_search because the match_results is undefined when a search fails. And also, the search flag should be regex_constants::match_default|regex_constants::match_not_dot_newline
Problem solved!
It sure does help to look at the documentation for the library you are using. Checking return values from functions is also quite helpful.


Postconditions: If the function returns false, then the effect on parameter m is undefined, otherwise the effects on parameter m are given in the table:

Concerning regex_search from http://www.boost.org/doc/libs/1_55_0/libs/regex/doc/html/boost_regex/ref/regex_search.html where m would correspond with titleMRes.

Topic archived. No new replies allowed.