代码改变世界

boost::regex

2015-10-27 21:16  编程小伙子  阅读(222)  评论(0编辑  收藏  举报

常见用法:

boost::regex express;

regex_match(str,express);  bool

smatch what;

cmatch what;

typedef match_results<const char*> cmatch;
typedef match_results<std::string::const_iterator> smatch;

regex_search(str,what,express);bool

what[0];

what[1];

regex_replace(str,express,kongge);

 

①匹配全串存在多个结果的情况下

主要针对string

boost::sregex_iterator ite(str.begin(), str.end(), express);
boost::sregex_iterator endite;
while (ite != endite)
{
cout<<*ite<<endl;
ite++;
}

char *p = "www.baidu.com---dffsfdsf--f-ff www.google.comddfdsffd";

boost::regex exp("\\w{3}\\.(\\w+\\.)+(net|com|cn)");

char *tmp = p;

cmath what;

while(boost::regex_search(tmp, what, exp))

{

  cout<<what[0]<<endl;

  tmp = what[0].second;

}

③可以通过const *p = s.c_str();

 

 

const_reference operator[](int n) const : 返回一个sub_match引用,当n=0时返回[first,last)的匹配结果,当0<n<=size时,返回对应元组的匹配结果,

当n>size时返回一个matched值为假的sub_match结构,元组按照左边括号的顺序和sub_match相对应,从1开始

 一篇写得不错的博文

 http://www.cnblogs.com/hucn/archive/2011/05/09/2041490.html

 

boost::regex iterator

http://www.boost.org/doc/libs/1_55_0/libs/regex/doc/html/boost_regex/ref/regex_iterator.html