《转》/usr/include/regex.h

在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);


#include <stdio.h>
#include <regex.h>

int changeFormat(char *str) {

        int     n, i;
        regex_t re;
        regmatch_t pm[10];
        char    buf[1024], pattern[1024];

        //strcpy(pattern, "(<[0-9]{1,2}>)([0-9]{4}-[0-9]{2}-([0-9]{2}) ([0-9]{2}-[0-9]{2}-[0-9]{2}) ([0-9]{1,2,3}.[0-9]{1,2,3}.[0-9]{1,2,3}.[0-9]{1,2,3}) ([0-9]{1,2}) ([0-9]{1,2})(//S)");
       //strcpy(pattern, "<([0-9]{1,2})>([0-9]{4}-[0-9]{2}-[0-9]{2}) ([0-9]{2}:[0-9]{2}:[0-9]{2}) ([0-9]{1,3}//.[0-9]{1,3}//.[0-9]{1,3}//.[0-9]{1,3}) (//S)");
       //strcpy(pattern, "<([0-9]{1,2})>([0-9]{4}-[0-9]{2}-[0-9]{2}) ([0-9]{2}:[0-9]{2}:[0-9]{2}) ([0-9]{1,3}//.[0-9]{1,3}//.[0-9]{1,3}//.[0-9]{1,3})");
       //strcpy(pattern, "<([0-9]{1,2})>([0-9]{4}-[0-9]{2}-[0-9]{2}) ([0-9]{2}:[0-9]{2}:[0-9]{2}) ([0-9]{1,3}//.[0-9]{1,3}//.[0-9]{1,3}//.[0-9]{1,3}) ([0-9]{1,2}) ([0-9]{1,2})");
       strcpy(pattern, "<([0-9]{1,2})>([0-9]{4}-[0-9]{2}-[0-9]{2}) ([0-9]{2}:[0-9]{2}:[0-9]{2}) ([0-9]{1,3}//.[0-9]{1,3}//.[0-9]{1,3}//.[0-9]{1,3}) ([0-9]{1,2}) ([0-9]{1,2})");

        n = regcomp(&re, pattern, REG_EXTENDED);

        if (n != 0) {

                regerror(n, &re, buf, sizeof(buf));

                fprintf(stderr, "err1:[%d]%s", n, buf);

                return n;

        }

        n = regexec(&re, str, sizeof(pm)/sizeof(regmatch_t), pm, 0);

        if (n != 0) {

                if (n != REG_NOMATCH) {

                        regerror(n, &re, buf, sizeof(buf));

                        fprintf(stderr, "err2:[%d]%s", n, buf);

                }
        else
        {
           
                regerror(n, &re, buf, sizeof(buf));

                fprintf(stderr, "err1:[%d]%s", n, buf);
        }

        } else {

                memset(buf, 0x00, sizeof(buf));

                strncpy(buf, str + pm[1].rm_so, pm[1].rm_eo - pm[1].rm_so);
                strcat(buf, "@");

                strncat(buf, str + pm[2].rm_so, pm[2].rm_eo - pm[2].rm_so);
                strcat(buf, "@");

        strncat(buf, str + pm[3].rm_so, pm[3].rm_eo - pm[3].rm_so);
                strcat(buf, "@");
           
        strncat(buf, str + pm[4].rm_so, pm[4].rm_eo - pm[4].rm_so);
                strcat(buf, "@");
           
        strncat(buf, str + pm[5].rm_so, pm[5].rm_eo - pm[5].rm_so);
                strcat(buf, "@");
           
        strncat(buf, str + pm[6].rm_so, pm[6].rm_eo - pm[6].rm_so);
                strcat(buf, "@");
           
        printf("%s/n",buf);

            printf("begin: %u/n",str);   
            printf("end: %s/n",str+pm[6].rm_so);   
                strcpy(str,buf);               
        }

        regfree(&re);


        return n;

}

int main()
{
    char log[1024];
    sprintf(log,"<14>2002-11-11 12:12:12 11.22.33.44 3 3 aaaa aaaaaa");
        changeFormat(log);
        printf("%s/n",log);
    return 1;   
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
将QT += core QT -= gui CONFIG += c++11 TARGET = UavRectifyLoadLIb CONFIG += console CONFIG -= app_bundle TEMPLATE = app SOURCES += main.cpp # The following define makes your compiler emit warnings if you use # any feature of Qt which as been marked deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS win32{ CONFIG(debug, debug|release){ DESTDIR = $$PWD/../../../../RasterManager/bin/Debug } else{ DESTDIR = $$PWD/../../../../RasterManager/bin/release } INCLUDEPATH += $$PWD/../../../include/gdal1101 DEPENDPATH += $$PWD/../../../include/gdal1101 } else{ CONFIG(debug, debug|release){ DESTDIR = $$PWD/../../../product/release32 } else{ DESTDIR = $$PWD/../../../product/release32 } } # You can also make your code fail to compile if you use deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 unix:!macx: LIBS += -L$$PWD/../../../product/release32/ -lUAVAutoRectifyMt -lUAVAutoRectify -lUAVAutoRectifyFi INCLUDEPATH += $$PWD/../include DEPENDPATH += $$PWD/../include unix:!macx: LIBS += -L$$PWD/../../../lib/opencvf249/ -lopencv_core unix:!macx: LIBS += -L$$PWD/../../../lib/opencvf249/ -lopencv_highgui unix:!macx: LIBS += -L$$PWD/../../../lib/opencvf249/ -lopencv_imgproc INCLUDEPATH += $$PWD/../../../lib/opencvf249 DEPENDPATH += $$PWD/../../../lib/opencvf249 unix:!macx: LIBS += -L$$PWD/../../../../../../../usr/local/lib/ -lopencv_core #unix:!macx: LIBS += -L$$PWD/../../../../../../../usr/local/lib/ -lopencv_highgui unix:!macx: LIBS += -L$$PWD/../../../../../../../usr/local/lib/ -lopencv_imgproc unix:!macx: LIBS += -L$$PWD/../../../../../../../usr/local/lib/ -lopencv_features2d unix:!macx: LIBS += -L$$PWD/../../../../../../../usr/local/lib/ -lopencv_imgcodecs INCLUDEPATH += $$PWD/../../../../../../../usr/local/include DEPENDPATH += $$PWD/../../../../../../../usr/local/include unix:!macx: LIBS += -L$$PWD/../../../product/release32/ -lDEMDriver unix:!macx: LIBS += -L$$PWD/../../../product/release32/ -lProjection unix:!macx: LIBS += -L$$PWD/../../../product/release32/ -lIImage_gC QMAKE_CXXFLAGS_RELEASE = $$QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO QMAKE_LFLAGS_RELEASE = $$QMAKE_LFLAGS_RELEASE_WITH_DEBUGINFO 为cmake格式
06-11

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值