Reputation: 343
I'm using the following function with Boost::tr1::sregex_token_iterator
int regexMultiple(std::string **s, std::string r)
{
std::tr1::regex term=(std::tr1::regex)r;
const std::tr1::sregex_token_iterator end;
int nCountOcurrences;
std::string sTemp=**s;
for (std::tr1::sregex_token_iterator i(sTemp.begin(),sTemp.end(), term); i != end; ++i)
{
(*s)[nCountOcurrences]=*i;
nCountOcurrences++;
}
return nCountOcurrences;
}
As you can suppose, **s
is a pointer to a string, and r is the regex in question. This function works (in fact, this one might not work because I modified it from the original just to make it simpler, given that the rest is not relevant to the question).
What I want to know is, given, for example, a regex of this kind: "Email: (.*?) Phone:..."
, is there any way to retrieve only the (.*?) part from it, or should I apply substrings over the given result to achieve this instead?
Else, it's going to throw out: Email: [email protected] Phone: ..
Thanks.
Upvotes: 0
Views: 1030
Reputation: 343
Should use regex_search like Kerrek SB
recommended instead: http://www.boost.org/doc/libs/1_39_0/libs/regex/doc/html/boost_regex/ref/regex_search.html
int regexMultiple(std::string **s, std::string r)
{
std::tr1::regex term=(std::tr1::regex)r;
std::string::const_iterator start, end;
boost::match_results<std::string::const_iterator> what;
int nCountOcurrences=0;
std::string sTemp=**s;
start=sTemp.begin();
end=sTemp.end();
boost::match_flag_type flags = boost::match_default;
while (regex_search(start,end, what, term, flags))
{
(*s)[nCountOcurrences]=what[1];
nCountOcurrences++;
start = what[0].second;
flags |= boost::match_prev_avail;
flags |= boost::match_not_bob;
}
return nCountOcurrences;
}
Upvotes: 2