C++中三种正则表达式比较_第1页
C++中三种正则表达式比较_第2页
C++中三种正则表达式比较_第3页
C++中三种正则表达式比较_第4页
C++中三种正则表达式比较_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、C+中三种正则表达式比较之后我变换了匹配的字符串,将其长度生了一倍,达到每个100字符左右(代码里面所示),匹配速度就下来了,但是也能达到 100w/s左右,这肯定满足我们现在的需求了。作者:pmars的博客来源:pmars的博客|2017-01-05 16:19 收藏  分享工作需要用到C+中的正则表达式,所以就研究了以上三种正则。1、C regex/* write by xingming * time:2012年10月19日15:51:53 * for: test regex * */#include <regex.h>#include <i

2、ostream>#include <sys/types.h>#include <stdio.h>#include <cstring>#include <sys/time.h>using namespace std;const int times = 1000000;int main(int argc,char* argv) char pattern512="||3.*(channel=finance|_finance$|ch=stock|/stoc

3、k/)| const size_t nmatch = 10; regmatch_t pm10; int z ; regex_t reg; char lbuf256="set",rbuf256; char buf3256 = " ".sina.egooooooooo", " printf("input strings:n"); timeval end,start; gettimeofday(&start,NULL); regcomp(&reg,pattern,REG_EXTENDED|REG_NOSU

4、B); for(int i = 0 ; i < times; +i) for(int j = 0 ; j < 3; +j) z = regexec(&reg,bufj,nmatch,pm,REG_NOTBOL);/* if(z=REG_NOMATCH) printf("no matchn"); else printf("okn"); */ gettimeofday(&end,NULL); uint time = (end.tv_sec-start.tv_sec)*1000000 + end.tv_usec - start.tv

5、_usec; cout<<time/1000000<<" s and "<<time%1000000<<" us."<<endl; return 0 ;使用正则表达式可简单的分成几步:· 1.编译正则表达式· 2.执行匹配· 3.释放内存首先,编译正则表达式int regcomp(regex_t *preg, const char *regex, int cflags);reqcomp()函数用于把正则表达式编译成某种格式,可以使后面的匹配更有效。preg: reg

6、ex_t结构体用于存放编译后的正则表达式;regex: 指向正则表达式指针;cflags:编译模式共有如下四种编译模式:REG_EXTENDED:使用功能更强大的扩展正则表达式REG_ICASE:忽略大小写REG_NOSUB:不用存储匹配后的结果REG_NEWLINE:识别换行符,这样$就可以从行尾开始匹配,就可以从行的开头开始匹配。否则忽略换行符,把整个文本串当做一个字符串处理。其次,执行匹配int regexec(const regex_t *preg, const char *string, size_t nmatch, regmatch_t pmatch, int eflags);pr

7、eg: 已编译的正则表达式指针;string:目标字符串;nmatch:pmatch数组的长度;pmatch:结构体数组,存放匹配文本串的位置信息;eflags:匹配模式共两种匹配模式:REG_NOTBOL:The match-beginning-of-line operator always fails to match  (but see  the  compilation  flag  REG_NEWLINE above). This flag may be used when different portions of a string

8、 are passed  to  regexec and the beginning of the string should not be interpreted as the beginning of the line.REG_NOTEOL:The match-end-of-line operator always fails to  match  (but  see the compilation flag REG_NEWLINE above)最后,释放内存void regfree(regex_t *preg);当使用完编译好的正则表达式

9、后,或者需要重新编译其他正则表达式时,一定要使用这个函数清空该变量。其他,处理错误size_t regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size);当执行regcomp 或者regexec 产生错误的时候,就可以调用这个函数而返回一个包含错误信息的字符串。errcode: 由regcomp 和 regexec 函数返回的错误代号。preg: 已经用regcomp函数编译好的正则表达式,这个值可以为NULL。errbuf: 指向用来存放错误信息的字符串的内存空间。errbuf_size: 指明b

10、uffer的长度,如果这个错误信息的长度大于这个值,则regerror 函数会自动截断超出的字符串,但他仍然会返回完整的字符串的长度。所以我们可以用如下的方法先得到错误字符串的长度。当然我在测试的时候用到的也比较简单,所以就直接用了,速度一会再说!2、C+ regex/* write by xingming * time:2012年10月19日15:51:53 * for: test regex * */#include <regex>#include <iostream>#include <stdio.h>#include <string>us

11、ing namespace std;int main(int argc,char* argv) regex pattern(":digit:",regex_constants:extended); printf("input strings:n"); string buf; while(cin>>buf) printf("*n%sn*n",buf.c_str(); if(buf = "quit") printf("quit just now!n"); break; match_res

12、ults<string:const_iterator> result; printf("run compare now! '%s'n", buf.c_str(); bool valid = regex_match(buf,result,pattern); printf("compare over now! '%s'n", buf.c_str(); if(!valid) printf("no match!n"); else printf("okn"); return 0 ;

13、C+这个真心不想多说它,测试过程中发现 字符匹配的时候 a 是可以匹配的,a+也是可以的,:w:也可以匹配任意字符,但:w:+就只能匹配一个字符,+号貌似不起作用了。所以后来就干脆放弃了这伟大的C+正则,如果有大牛知道这里面我错在哪里了,真心感谢你告诉我一下,谢谢。3、boost regex/* write by xingming * for:test boost regex * time:2012年10月23日11:35:33 * */ #include <iostream> #include <string> #include <sys/time.h

14、>#include "boost/regex.hpp"using namespace std;using namespace boost;const int times = 10000000;int main() regex pattern("||3.*(channel=finance|_finance$|ch=stock|/stock/)|dp.s /.*ch=9&"); cout<<"input strings:&quo

15、t;<<endl; timeval start,end; gettimeofday(&start,NULL); string input = " ".sina.egooooooooo", " for(int i = 0 ;i < times; + i) for(int j = 0 ; j < 3;+j) /if(input="quit") / break; /cout<<"string:'"<<input<<'''&

16、lt;<endl; cmatch what; if(regex_search(inputj.c_str(),what,pattern) ; / cout<<"OK!"<<endl; else ; / cout<<"error!"<<endl; gettimeofday(&end,NULL); uint time = (end.tv_sec-start.tv_sec)*1000000 + end.tv_usec - start.tv_usec; cout<<time/1000000&

17、lt;<" s and "<<time%1000000<<" us."<<endl; return 0 ;boost正则不用多说了,要是出去问,C+正则怎么用啊?那90%的人会推荐你用boost正则,他实现起来方便,正则库也很强大,资料可以找到很多,所以我也不在阐述了。4、对比情况总结:C regex的速度让我吃惊啊,相比boost的速度,C regex的速度几乎要快上3倍,看来正则引擎的选取上应该有着落了!上面的表格中我用到的正则和字符串是一样的(在代码中C regex的被我加长了),速度相差几乎有3倍,C的速

18、度大约在30+w/s , 而boost的速度基本在15-w/s ,所以对比就出来了!在这里Cregex的速度很让我吃惊了已经,但随后我的测试更让我吃惊。我以前在.net正则方面接触的比较多,就写了一个.net版本的作为对比using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Text.RegularExpressions;namespace 平常测试 class Program static int times = 1000000; static void M

19、ain(string args) Regex reg = new Regex("(?>||3.*(?:channel=finance|_finance$|ch=stock|/stock/)| string str = new string" ".sina.egooooooooo", " int tt = 0; DateTime start = DateTime.Now; for (int i = 0; i < times; +i) for (int j = 0; j < 3; +j) if (reg.IsMatch(strj) ; /Console.WriteLine("OK!")

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论