## page was renamed from zhSoftDespTemplate ##language:zh ''' 含有章节缩影的FLOSS中文 文章模板 ''' ::-- hoxide [<>] <> == libregex == === 简介 === GNU C Library 中的正则表达式库 === 官方页面 === === 文档 === === 例子 === {{{ #!cplusplus #include #include #include const char *p[] = { "abc" , "abc", "a.c" , "a?c", "acc", "a[bc]*eefc", "a[d"}; const char *s[] = { "abc", "ABC", "abc", "abc", "acd", "acbbccbeefcjkjl", "adcx"}; regex_t cp[sizeof(p)/sizeof(char *)]; char *get_regerror (int errcode, regex_t *compiled) { size_t length = regerror (errcode, compiled, NULL, 0); char *buffer = malloc (length); (void) regerror (errcode, compiled, buffer, length); return buffer; } int main() { int n = sizeof(p)/sizeof(char *); int i, error; for(i=0; i=0 && resm[i].rm_eo>=0) { printf("metch substring:\n\""); fwrite(&sp[resm[i].rm_so], resm[i].rm_eo-resm[i].rm_so, 1 ,stdout); printf("\"\nso: %d, eo:%d\n", resm[i].rm_so, resm[i].rm_eo); } } else { printf("pattern: %s have some error, errorcode : %s\n", hp, get_regerror(error, &chp)); } return 0; } }}} 结果(那个处理url的正则表达式可花了我不少时间, 注意re库默认使用POSIX Basic RE而平时常用的是POSIX Extended RE, 偶就错在这里): {{{ G:\my pro\projects\learning\c\libc>gcc -o re1 re1.c G:\my pro\projects\learning\c\libc>re1 pattern: abc , string abc match pattern: abc , string ABC not match pattern: a.c , string abc match pattern: a?c , string abc not match pattern: acc , string acd not match pattern: a[bc]*eefc , string acbbccbeefcjkjl match pattern: a[d have error: brackets ([ ]) not balanced pattern: abc , string ABC (with FLAG_ICASE) match pattern: http://(([^./]+)(\.([^./]+))*)/ string http://wiki.woodpecker.org/moin/Hoxide metch substring: "http://wiki.woodpecker.org/" so: 0, eo:27 metch substring: "wiki.woodpecker.org" so: 7, eo:26 metch substring: "wiki" so: 7, eo:11 metch substring: ".org" so: 22, eo:26 metch substring: "org" so: 23, eo:26 }}} === 讨论 ===