boost regex sub-string match

匿名 (未验证) 提交于 2019-12-03 08:46:08

问题:

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;   } } 

回答1:

The boost::regex_match only matches the whole string, you probably want boost::regex_search instead.



回答2:

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).



回答3:

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; } 

}



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!