原文链接:http://www.codeceo.com/article/cpp-regex-compare.html
工作需要用到C++中的正则表达式,所以就研究了以上三种正则。
1、C regex
#include #include #include #include #include #include using namespace std;const int times = 1000000;int main(int argc,char** argv){ char pattern[512]="finance.sina.cn|stock1.sina.cn|3g.sina.com.cn.*(channel=finance|_finance$|ch=stock|/stock/)|dp.sina.cn/.*ch=9&"; const size_t nmatch = 10; regmatch_t pm[10]; int z ; regex_t reg; char lbuf[256]="set",rbuf[256]; char buf[3][256] = {"finance.sina.cn/google.com/baidu.com.google.sina.cndddddddddddddddddddddda.sdfasdfeoasdfnahsfonadsdf", "3g.com.sina.cn.google.com.dddddddddddddddddddddddddddddddddddddddddddddddddddddbaidu.com.sina.egooooooooo", "http://3g.sina.com.cn/google.baiduchannel=financegogo.sjdfaposif;lasdjf.asdofjas;dfjaiel.sdfaosidfj"}; printf("input strings:n");
timeval end,start;
gettimeofday(&start,NULL);
regcomp(®,pattern,REG_EXTENDED|REG_NOSUB); for(int i = 0 ; i < times; ++i)
{ for(int j = 0 ; j < 3; ++j)
{
z = regexec(®,buf[j],nmatch,pm,REG_NOTBOL);
}
}
gettimeofday(&end,NULL);
uint time = (end.tv_sec-start.tv_sec)*1000000 + end.tv_usec - start.tv_usec; cout<