Reputation: 2011
I want to return output "match" if the pattern "regular" is a sub-string of variable st. Is this possible?
int main()
{
string st = "some regular expressions are Regxyzr";
boost::regex ex("[Rr]egular");
if (boost::regex_match(st, ex))
{
cout << "match" << endl;
}
else
{
cout << "not match" << endl;
}
}
Upvotes: 4
Views: 10522
Reputation: 1015
Your question is answered with example in library documentation - boost::regex
Alternate approach:
You can use boost::regex_iterator, this is useful for parsing file etc.
string[0],
string[1]
below indicates start and end iterator.
Ex:
boost::regex_iterator stIter(string[0], string[end], regExpression)
boost::regex_iterator endIter
for (stIter; stIter != endIter; ++stIter)
{
cout << " Whole string " << (*stIter)[0] << endl;
cout << " First sub-group " << (*stIter)[1] << endl;
}
}
Upvotes: 0
Reputation: 881517
regex_search does what you want; regex_match is documented as
determines whether a given regular expression matches all of a given character sequence
(the emphasis is in the original URL I'm quoting from).
Upvotes: 7
Reputation: 73470
The boost::regex_match only matches the whole string, you probably want boost::regex_search instead.
Upvotes: 15