含有章节缩影的FLOSS中文 文章模板

::-- hoxide [2005-07-30 12:48:24]

1. libregex

1.1. 简介

1.2. 官方页面

1.3. 文档

1.4. 例子

   1 #include<stdio.h>
   2 #include<stdlib.h>
   3 #include<regex.h>
   4 
   5 const char *p[] = { "abc" , "abc", "a.c" , "a?c", "acc", "a[bc]*eefc", "a[d"};
   6 const char *s[] = { "abc", "ABC", "abc", "abc", "acd", "acbbccbeefcjkjl", "adcx"};
   7 
   8 regex_t cp[sizeof(p)/sizeof(char *)];
   9 
  10 char *get_regerror (int errcode, regex_t *compiled)
  11 {
  12   size_t length = regerror (errcode, compiled, NULL, 0);
  13   char *buffer = malloc (length);
  14   (void) regerror (errcode, compiled, buffer, length);
  15   return buffer;
  16 }
  17 
  18 
  19 int main()
  20 {
  21   int n = sizeof(p)/sizeof(char *);
  22   int i, error;
  23 
  24   for(i=0; i<n; i++)
  25     {
  26       error=regcomp(& cp[i], p[i], 0);
  27       if(!error)
  28         {
  29           printf("pattern: %s , string %s\n", p[i],s[i]);
  30           printf("%s\n",  regexec(&cp[i],s[i],0, NULL , 0)?
  31                  "not match":"match");
  32           regfree (&cp[i]);
  33         }
  34       else
  35         {
  36           printf("pattern: %s have error: %s\n", 
  37                  p[i], get_regerror(error, &cp[i]));
  38         }
  39     }
  40   regcomp(&cp[1], p[1], REG_ICASE);
  41   printf("pattern: %s , string %s (with FLAG_ICASE)\n", p[1],s[1]);
  42   printf("%s\n",  regexec(&cp[1],s[1],0, NULL , 0)?
  43                  "not match":"match");
  44   regfree (&cp[1]);
  45 
  46   char hp[] = "http://(([^./]+)(\\.([^./]+))*)/";
  47   char sp[] = "http://wiki.woodpecker.org/moin/Hoxide";
  48   regex_t chp;
  49   const nresm=10;
  50   regmatch_t resm[nresm];
  51   printf("pattern: %s \n" , hp);
  52   error=regcomp(&chp, hp, REG_EXTENDED);
  53   if(!error)
  54     {
  55       printf("string %s\n",sp);
  56       if(!regexec(&chp,sp, nresm , resm, 0))
  57         for(i=0; i<nresm;i++)
  58           if(resm[i].rm_so>=0 && resm[i].rm_eo>=0)
  59             {
  60               printf("metch substring:\n\"");
  61               fwrite(&sp[resm[i].rm_so], 
  62                      resm[i].rm_eo-resm[i].rm_so, 1 ,stdout);
  63               printf("\"\nso: %d, eo:%d\n", resm[i].rm_so, resm[i].rm_eo);
  64             }
  65     }
  66   else
  67     {
  68       printf("pattern: %s have some error, errorcode : %s\n",
  69              hp, get_regerror(error, &chp));
  70     }
  71   
  72 
  73   return 0;
  74 }

结果(那个处理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

1.5. 讨论

glibc/regex (last edited 2009-12-25 07:11:16 by localhost)