Saturday, January 06, 2007

regexec(3)'s usage:Match Results with Subexpressions

http://www.gnu.org/software/libc/manual/html_node/Regexp-Subexpressions.html#Regexp-Subexpressions
An example program:
#include <stdio.h>
#include <sys/types.h>
#include <regex.h>

/* 取子串的函数 */
static char* substr(const char*str,
                unsigned start, unsigned end)
{
        unsigned n = end - start;
        static char stbuf[256];
        strncpy(stbuf, str + start, n);
        stbuf[n] = 0;
        return stbuf;
}
/* 主程序 */
int main(int argc, char** argv)
{
        int x, z, lno = 0, cflags = REG_EXTENDED;
        char ebuf[128], lbuf[256];
        regex_t reg;
        regmatch_t pm[10];
        const size_t nmatch = 10;
        /* 编译正则表达式*/
        z = regcomp(&reg, "(reg|regular)[ ]?(ex|expression)", cflags);
        /* 逐行处理输入的数据 */
        while(fgets(lbuf, sizeof(lbuf), stdin))
        {
                ++lno;
                if ((z = strlen(lbuf)) > 0 && lbuf[z-1]
                                == '\n')
                        lbuf[z - 1] = 0;
                /* 对每一行应用正则表达式进行匹配 */
                z = regexec(&reg, lbuf, nmatch, pm, 0);
                if (z == REG_NOMATCH) continue;
                else if (z != 0) {
                        regerror(z, &reg, ebuf, sizeof(ebuf));
                        fprintf(stderr, "%s: regcom('%s')\n",
                                        ebuf, lbuf);
                        return 2;
                }
                /* 输出处理结果 */
                for (x = 0; x < nmatch && pm[x].rm_so != -1; ++ x)
                {
                        if (!x) printf("%04d: %s\n", lno, lbuf);
                        printf(" $%d='%s'\n", x, substr(lbuf, pm[x].rm_so,
                                                pm[x].rm_eo));
                }
        }
        /* 释放正则表达式 */
        regfree(&reg);
        return 0;
}
Run the program
$ ./a.out
regex
0001: regex
 $0='regex'
 $1='reg'
 $2='ex'
regular expresion
0002: regular expresion
 $0='regular ex'
 $1='regular'
 $2='ex'
regular expression
0003: regular expression
 $0='regular expression'
 $1='regular'
 $2='expression'


0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home