user-0 提问: 想问问下面这段数据文件怎么用正则表达式匹配出结果？（用C语言写）

回复:
user: user-1
content: Linux版本更新一下， Windows版本在你另一个帖子上http://bbs.csdn.net/topics/391073876  /* on linux using posix regex */ #include<stdio.h> #include<stdlib.h> #include<string.h> #include <regex.h>  #define MAX_MATCH_NUM (5) /* all,date,op,bytes,cliID */  void main(void) {     regex_t reg;     regmatch_t match[MAX_MATCH_NUM];     char pattern[] = "(^[^,]+).+bytes: ([^,]+).+op: ([^,]+).+cliID: DFSClient_attempt([^,]+)_m";     char result[256] = {0};     int errcode = 0;     /* compile pattern */     errcode = regcomp(®, pattern, REG_EXTENDED);     if (errcode != 0)     {/* print human readable error prompt */         char errbuff[64];         regerror(errcode, ®, errbuff, sizeof(errbuff));         printf("regcomp failed,%s\n", errbuff);         exit(EXIT_FAILURE);     }      /* do match */     errcode = regexec(®, string, MAX_MATCH_NUM, match, 0);     if (0 == errcode)     {         /* op */         strncat(result, string + match[3].rm_so, \             match[3].rm_eo - match[3].rm_so);         strcat(result, "|");          /* date */         strncat(result, string + match[1].rm_so, \             match[1].rm_eo - match[1].rm_so);         strcat(result, "|");          /* application */         strcat(result, "application");         strncat(result, string + match[4].rm_so, \                 match[4].rm_eo - match[4].rm_so);         strcat(result, "|");          /* bytes */         strncat(result, string + match[2].rm_so, \             match[2].rm_eo - match[2].rm_so);          printf("result = %s\n", result);     }      else if (REG_NOMATCH == errcode)     {         printf("no match\n");     }     else     {/* print human readable error prompt */         char errbuff[64];         regerror(errcode, ®, errbuff, sizeof(errbuff));         printf("regcomp failed,%s\n", errbuff);         regfree(®);         exit(EXIT_FAILURE);     }      /* free regex */     regfree(®);     exit(EXIT_SUCCESS); }

user: user-2
content: 正则表达式不是万能的； 有限状态自动机是万能的。

user: user-0
content: 想问下大家，如何把这些截取字段和分割符是写在配置文件中，打算从程序中读取配置文件，然后传入程序进行匹配截取，这样的话，程序感觉匹配难度会加大很多，求赐教思路

user: user-0
content: @ lgbxyz #include <regex.h> 这个头文件Linux下才能用，Windows下无法包含呢

user: user-1
content: 把64行放到50行上面

user: user-1
content: /* on linux using posix regex */ #include<stdio.h> #include<stdlib.h> #include<string.h> #include <regex.h>  #define MAX_MATCH_NUM (5) /* all,date,op,bytes,cliID */  void main(void) { 	regex_t reg; 	regmatch_t match[MAX_MATCH_NUM]; 	 	char string[] = "2015-07-07 15:42:52,666 INFO org.apache.hadoop.hdfs.server.datanode.DataNode.clienttrace: src: /10.215.145.89:9003, dest: /10.196.139.31:38385, bytes: 17660496, op: HDFS_READ, cliID: DFSClient_attempt_1435652218972_8928036_m_000228_0_1283375306_1, offset: 0, srvID: DS-885471072-10.215.145.89-9003-1416312428093, blockid: BP-1531210715-10.215.135.49-1416303685693:blk_1256393234_183414267, duration: 32839633797"; 	char pattern[] = "(^[^,]+).+(bytes: [^,]+).+(op: [^,]+).+(cliID: [^,]+_m)"; 	char result[256] = {0};   	int errcode = 0; 	    /* compile pattern */     errcode = regcomp(®, pattern, REG_EXTENDED);     if (errcode != 0)     {/* print human readable error prompt */         char errbuff[64];         regerror(errcode, ®, errbuff, sizeof(errbuff));         printf("regcomp failed,%s\n", errbuff);         exit(EXIT_FAILURE);     }     	 	/* do match */     errcode = regexec(®, string, MAX_MATCH_NUM, match, 0);     if (0 == errcode)     { 		/* op */ 		strncat(result, string + match[3].rm_so + strlen("op: "), \ 			match[3].rm_eo - match[3].rm_so - strlen("op: ")); 		strcat(result, "|"); 		/* date */ 		strncat(result, string + match[1].rm_so, \ 			match[1].rm_eo - match[1].rm_so); 		strcat(result, "|"); 		/* application */ 		strcat(result, "application"); 		strncat(result, string + match[4].rm_so + strlen("cliID: DFSClient_attempt"), \ 				match[4].rm_eo - match[4].rm_so- strlen("cliID: DFSClient_attempt") - strlen("_m")); 		strcat(result, "|"); 		/* bytes */ 		strncat(result, string + match[2].rm_so + strlen("bytes: "), \ 			match[2].rm_eo - match[2].rm_so - strlen("bytes: "));		     }     else if (REG_NOMATCH == errcode)     {         printf("no match\n");     }     else     {/* print human readable error prompt */         char errbuff[64];         regerror(errcode, ®, errbuff, sizeof(errbuff));         printf("regcomp failed,%s\n", errbuff);         regfree(®);         exit(EXIT_FAILURE);     } 	 	printf("result = %s\n", result);      /* free regex */     regfree(®);     exit(EXIT_SUCCESS); }

user: user-3
content: 感觉一个正则表达式无法匹配出这么多的字符串