- 论坛徽章:
- 0
|
- /*
- author:Minuit
- Date:Tue 04 Jul 2006 05:08:05 AM EDT
- description: RE
- */
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include<regex.h>
- #define nmatch 5
- #define BUFSIZE 80
- void retest(char *string)
- {
- regex_t preg;
- char *regex="[\\ ][a-zA-Z]+\\>"; //正则
- regmatch_t pmatch[nmatch];
- unsigned int i,len;
- bzero(&preg,sizeof(regex_t));
- if(regcomp(&preg,regex,REG_EXTENDED)==0)
- {
- if(regexec(&preg,string,nmatch,pmatch,0)==0)
- {
- for(i=0;i<nmatch;i++)
- {
- if(pmatch[i].rm_so==-1)continue;
- len=(pmatch[i].rm_eo-pmatch[i].rm_so);
- printf("匹配结果:%.*s\n",len,(string+pmatch[i].rm_so));
- }
- }
- regfree(&preg);
- }
- }
- int main()
- {
- FILE *fp;
- char buf[BUFSIZE];
- if((fp=fopen("txt","r"))==NULL)
- {
- fprintf(stderr,"不能打开文件\n");
- exit(1);
- }
- while(fgets(buf,BUFSIZE,fp))
- {
- printf("%s",buf);
- retest(buf);
- }
- fclose(fp);
- return 0;
- }
复制代码
文本
- [111111 2005-09-18 12-0i-22 testRegx]
- [132411 2005-09-18 12-18-22 erfogfdx]
- [111df1 2005-09-18 12-08-22 fsffRegx]if
- [111111 2005-09-18 12-08-22 fdsfRegx]ix
- [111111 2005-09-18 12-08-22 idfsRegx]ixxx
- [111111 2005-09-18 12-08-22 idfsReg]xxxx
复制代码
结果
- minuit@suse:~/c++> ./regex
- [111111 2005-09-18 12-0i-22 testRegx]
- 匹配结果: testRegx
- [132411 2005-09-18 12-18-22 erfogfdx]
- 匹配结果: erfogfdx
- [111df1 2005-09-18 12-08-22 fsffRegx]if
- 匹配结果: fsffRegx
- [111111 2005-09-18 12-08-22 fdsfRegx]ix
- 匹配结果: fdsfRegx
- [111111 2005-09-18 12-08-22 idfsRegx]ixxx
- 匹配结果: idfsRegx
- [111111 2005-09-18 12-08-22 idfsReg]xxxx
- 匹配结果: idfsReg
- minuit@suse:~/c++>
复制代码
不知道HP上面有没有这个正则头文件
以前写的就改了一下正则将就用 |
|