Chinaunix首页 | 论坛 | 博客
  • 博客访问: 100512
  • 博文数量: 39
  • 博客积分: 2000
  • 博客等级: 大尉
  • 技术积分: 318
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-09 10:01
文章分类

全部博文(39)

文章存档

2011年(7)

2010年(20)

2009年(12)

我的朋友

分类: C/C++

2009-11-17 19:31:47

GNU C 規則表達式入門
本文出自: 作者:Tong Jian(tongjian@staff.sina.com.cn) (2001-09-05 09:00:00)
       用過Perl的朋友應該都它提供了規則表達式功能,所以使得用perl 進行文本處理非常方便。本人最近試用了一下
       GNU C 的規則表達式功能, 
       發現使用起來也很簡單,只是覺得支持的不夠強大,不能夠進行文本的替換,只能進行文本的查找,至少目前我是
       沒有發現基規則表達式的 
       文本替換功能。 

       下面我就簡單的介紹一下GNU C 的規則表達式使用方法,有理解不對的地方,還請朋友們多指正。 

       在GNU C 中要使用規則表達式,需要用到以下幾個函數。(定義在/usr/include/regex.h文件中) 
       * int regcomp (regex_t *compiled, const char *pattern, int cflags) 
       * int regexec (regex_t *compiled, char *string, size_t nmatch, regmatch_t matchptr [], int
       eflags) 
       * void regfree (regex_t *compiled) 
       * size_t regerror (int errcode, regex_t *compiled, char *buffer, size_t length) 

       下面我就介紹分別一下這幾個函數和它用到的一些數據類型。 

       1.int regcomp (regex_t *compiled, const char *pattern, int cflags) 

       這個函數把指定的規則表達式pattern編譯成一種特定的數據格式compiled,這樣可以使匹配更有效。函數
       regexec 會使用這個數據在目標文 
       本串中進行模式匹配。執行成功返回0。 

       regex_t 是一個結構體數據類型,用來存放編譯的規則表達式,它的成員re_nsub 用來存儲規則表達式中的子
       規則表達式的個數,子規則表 
       達式就是用圓括號包起來的部分表達式。 

       pattern 是指向我們寫好的規則表達式的指針。 
       cflags 有如下4個值或者是它們或運算(|)的值: 
       REG_EXTENDED 以功能更加強大的擴展規則表達式的方式進行匹配。 
       REG_ICASE 匹配字母時忽略大小寫。 
       REG_NOSUB 不用存儲匹配的結果。 
       REG_NEWLINE 識別換行符,這樣'$'就可以從行尾開始匹配,'^'就可以從行的開頭開始匹配。 

       2. int regexec (regex_t *compiled, char *string, size_t nmatch, regmatch_t matchptr [], int
       eflags) 

       當我們編譯好規則表達式,就可以用regexec 匹配我們的目標文本串了,如果在編譯規則表達式的時候沒有指
       定cflags的參數為 
       REG_NEWLINE,則默認情況下是忽略換行符的,也就是把整個文本串當作一個字符串處理。執行成功返回0。 

       regmatch_t 是一個結構體數據類型,成員rm_so 存放匹配文本串在目標串中的開始位置,rm_eo 存放結束位
       置。通常我們以數組的形式定義 
       一組這樣的結構。因為往往我們的規則表達式中還包含子規則表達式。數組0單元存放主規則表達式位置,邊的
       單元依次存放子規則表達式位 
       置。 

       compiled 是已經用regcomp函數編譯好的規則表達式。 
       string 是目標文本串。 
       nmatch 是regmatch_t結構體數組的長度。 
       matchptr regmatch_t類型的結構體數組,存放匹配文本串的位置信息。 
       eflags 有兩個值 
       REG_NOTBOL 按我的理解是如果指定了這個值,那'^'就不會從我們的目標串開始匹配。總之我到現在還不是很
       明白這個參數的意義, 
       原文如下: 
       If this bit is set, then the beginning-of-line operator doesn't match the beginning of the
       string (presumably 
       because it's not the beginning of a line).If not set, then the beginning-of-line operator
       does match the beginning 
       of the string. 
       REG_NOTEOL 和上邊那個作用差不多,不過這個指定結束end of line。 

       3. void regfree (regex_t *compiled) 

       當我們使用完編譯好的規則表達式,或者要重新編譯其他規則表達式的時候,我們可以用這個函數清空
       compiled指向的regex_t結構體的內 
       容,請記住,如果是重新編譯的話,一定要先清空regex_t結構體。 

       4. size_t regerror (int errcode, regex_t *compiled, char *buffer, size_t length) 

       當執行regcomp 或者regexec 產生錯誤的時候,就可以調用這個函數而返回一個包含錯誤信息的字符串。 

       errcode 是由regcomp 和 regexec 函數返回的錯誤代號。 
       compiled 是已經用regcomp函數編譯好的規則表達式,這個值可以為NULL。 
       buffer 指向用來存放錯誤信息的字符串的內存空間。 
       length 指明buffer的長度,如果這個錯誤信息的長度大這個值,則regerror 函數會自動截斷超出的字符串,
       但他仍然會返回完整的字符 
       串的長度。所以我們可以用如下的方法先得到錯誤字符串的長度。 
       size_t length = regerror (errcode, compiled, NULL, 0); 

       好了,下面來實戰一下,這樣你就會理解的更清楚了。 
       --------------------------------------------------------------- 

       /* regex_test.c 
       * regular expression test in GNU C 
       * 
       * tested on redhat6.1 
       * gcc regex_test.c -o regex_test 
       */ 

       #include 
       #include 
       #include 
       #include 
       #include 

       #define SUBSLEN 10 
       #define EBUFLEN 128 /* error buffer length */ 
       #define BUFLEN 1024 /* matched buffer length */ 

       int 
       main (int argc, char **argv) 
       { 
       FILE *fp; 
       size_t len; /* store error message length */ 
       regex_t re; /* store compilned regular expression */ 
       regmatch_t subs[SUBSLEN]; /* store matched string position */ 
       char matched[BUFLEN]; /* store matched strings */ 
       char errbuf[EBUFLEN]; /* store error message */ 
       int err, i; 

       char string[] = "AAAAabaaababAbAbCdCd123123 11(123){12}"; 
       char pattern[] = "(\\([0-9]+\\))(\\{[0-9]+\\}{1})$"; 

       printf ("String : %s\n", string); 
       printf ("Pattern: \"%s\"\n", pattern); 

       /* compile regular expression */ 
       err = regcomp (&re, pattern, REG_EXTENDED); 

       if (err) 
       { 
       len = regerror (err, &re, errbuf, sizeof (errbuf)); 
       fprintf (stderr, "error: regcomp: %s\n", errbuf); 
       exit (1); 
       } 
       printf ("Total has subexpression: %d\n", re.re_nsub); 

       /* execute pattern match */ 
       err = regexec (&re, string, (size_t)SUBSLEN, subs, 0); 

       if (err == REG_NOMATCH) 
       { 
       fprintf (stderr, "Sorry, no match ...\n"); 
       regfree (&re); 
       exit (0); 
       } 
       else if (err) 
       { 
       len = regerror (err, &re, errbuf, sizeof (errbuf)); 
       fprintf (stderr, "error: regexec: %s\n", errbuf); 
       exit (1); 
       } 

       /* if no REG_NOMATCH and no error, then pattern matched */ 
       printf ("\nOK, has matched ...\n\n"); 
       for (i = 0; i <= re.re_nsub; i++) 
       { 
       if (i == 0) 
       { 
       printf ("begin: %d, end: %d, ", 
       subs.rm_so, subs.rm_eo); 
       } 
       else 
       { 
       printf ("subexpression %d begin: %d, end: %d, ", 
       i, subs.rm_so, subs.rm_eo); 
       } 
       len = subs.rm_eo - subs.rm_so; 
       memcpy (matched, string + subs.rm_so, len); 
       matched[len] = '\0'; 
       printf ("match: %s\n", matched); 
       } 

       regfree(&re); 
       exit(0); 
       } 
阅读(550) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~