
I am using regex and am having some weird returns. I can assign my results to a std:vector <std:string>. The vector[0] does contain the correct match, but vector[1] does not contain what is in a set of parentheses as it should. In fact I usually get a segment fault when I try to access it.
I then found I can access results from the grep by using the boost::Regex.What(n). However What(0) returns the next available match in the string, and What(1) and up will correctly report the substrings defined in this second match correctly. If no second match is available then the What() member will return the original match.
How do I fix the vector problem? OR? Does using What() force another search to take place? And if so can I change that behavior?
You are using class RegEx incorrectly: RegEx::Grep finds all the matches of the expression in the text given and for each match pushes $& onto the vector - if there is only one match found then there will be only one string in the vector (check the return value or the vectors size to find out how many there are), it does *not* push $1 $2 etc onto the vector. After a call to RegEx::Grep then RegEx::What returns the last match that was found in the string (with What(1) containing $1 for the last match found etc). The following is a rewritten version of your code that actually compiles (!) and does the right thing: #include <boost/cregex.hpp> #include <iostream> using namespace std; int main() { string reg_string ="[0-9]{3}(-[0-9]{2}-)[0-9]{4}"; //yes it is a SSN string search_string = "Big Bird \n123 sesame street\n the bronx NY 11722\n545-65-2343\nmorestuff\nandevenmore\nSnuff ELufigous \n 234 sesame street \n the bronx NY 11722\n889-99-5527\nmorestuff\nandevenmore\n"; std::vector<std::string> results; boost::RegEx regsearch(reg_string,false); int num_matchs; num_matchs=regsearch.Grep(results, search_string, boost::match_any); cout << num_matchs << " Number of Matchs\n"; // this is 2 for(int i = 0; i < num_matchs; ++i) { cout << "Match " << i << " is " << results[i] << "\n"; } } John.