C/C++正则表达式编程

正则表达式是一种描述性的语言, 用来概括一类字符串 (或者说一个字符串集合).

正则表达式30分钟入门教程

http://deerchao.net/tutorials/regex/regex.htm

GNU/Linux有两套库可用于正则表达式编程:POSIX库和PCRE库。前者不需要单独安装,一般需求还是能满足的,速度稍慢些。后者是久负盛名的Perl正则表达式库,功能强大,匹配速度快,不过可能需要单独安装。

POSIX库

如何使用POSIX库的例子:

#include <stdio.h>
#include <sys/types.h>
#include <regex.h>

char *get_regerror (int errcode, regex_t *compiled)
{
size_t length = regerror (errcode, compiled, NULL, 0);
char *buffer = malloc(length);
if (!buffer) return NULL;
(void) regerror (errcode, compiled, buffer, length);
return buffer;
}

int regtest(const char*pattern, const char* string)
{
regex_t reg;
regmatch_t *subexprs = NULL;
int ret;
int i;

if (0 != (ret=regcomp(?, pattern, REG_EXTENDED))) {
   char *buffer = get_regerror(ret, ?);
   if (buffer) {
    fprintf(stderr, "regcomp:[%d]%s/n", ret, buffer);
    free(buffer);
   }

   return -1;
}

subexprs = malloc((reg.re_nsub+1)*sizeof(regmatch_t));
if (!subexprs) {
   fprintf(stderr, "error malloc subexprs/n");
   regfree(?);
   return -1;
}

if (0 != (ret=regexec(?, string, reg.re_nsub+1, subexprs, 0))) {
   char *buffer = get_regerror(ret, ?);
   if (buffer) {
    fprintf(stderr, "regexec:[%d]%s/n", ret, buffer);
    free(buffer);
   }
  
   regfree(?);
   return -1;
}

for (i = 0; i <= reg.re_nsub; i++)   {
   printf("[%d]:", i);
  
   if (subexprs[i].rm_so == subexprs[i].rm_eo) {
    printf("[EMPTY SUBEXPR]/n");
   }
   else if (subexprs[i].rm_so == -1 ||
    subexprs[i].rm_eo == -1) {
    printf("[NO SUBEXPR]/n");
   }
   else   {
    fwrite(string+subexprs[i].rm_so, 1,
     subexprs[i].rm_eo-subexprs[i].rm_so, stdout);
    printf("/n");
   }
}

regfree(?);
if (subexprs) free(subexprs);
return 0;
}

int main(int argc, char *argv[])
{
if (argc != 3) {
   fprintf(stderr, "Usage: regtest pattern string/n");
   return -1;
}

fprintf(stderr, "pattern:%s/n", argv[1]);
fprintf(stderr, "string:%s/n", argv[2]);

return regtest(argv[1], argv[2]);
}

在字符串匹配之前,必须先编译匹配模式,这是通过regcomp实现的。这个函数的原型如下:

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

参数compiled有一个成员需要关注:re_nsub,代表编译后的子表达式数目,由于需要保存整个匹配到的模式,所以最终匹配的条目数是re_nsub加1。cflags用来修饰匹配模式,可取值如下:

REG_EXTENDED 启用POSIX正则库扩展,关于该扩展的详细信息可参考POSIX规范
REG_ICASE 忽略大小写
REG_NOSUB 不要存储子表达式
REG_NEWLINE 把换行符作为多行的分隔符,这样'$'可匹配每一行的行尾,'^'匹配每一行的行首,'.'不匹配换行符,[^...]不匹配新行

编译完模式后从内存中分配子表达式存储空间,然后调用regexec对串进行匹配,该函数原型如下:

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

nmatch指明matchptr数组的数目,该数目是compiled->re_nsub+1,也可以让nmatch为0,matchptr为NULL,表示不要保存子表达式。eflags通常为0。

匹配结束后,匹配到的子表达式在串中的偏移保存在regmatch_t结构中,该结构有两个成员:

rm_so 子表达式的起始偏移
rm_eo 子表达式的结束偏移

这是一个开区间,实际的子表达式在[rm_so,rm_eo)里。

如果没有匹配的子表达式,比如"f(o*)"匹配"fum",实际匹配到的只有"f",这时rm_so和rm_eo相等,都为1。如果整个模式在没有子表示式的情况下也能匹配,这时rm_so和rm_eo为-1,比如"ba(na)*"匹配"ba"。

 

PCRE库

PCRE库的功能虽然强大,可是并不难使用。详细信息可参考http://www.pcre.org/里的文档,附录有一个例子,比较全面地阐述了如何调用该库。

PCRE的子表达式和POSIX类似,不过它还引入了一个命名子表达式的概念。比如模式"(?P(?P(/d/d)?/d/d)-(?P/d/d)-(?P/d/d))",其中的date, year, month, day是对子表达式的命名。如何根据这些名称来获取子表达式,PCRE文档有详细的说明。

附录:pcredemo.c

/*************************************************
*            PCRE DEMONSTRATION PROGRAM            *
*************************************************/

/* This is a demonstration program to illustrate the most straightforward ways
of calling the PCRE regular expression library from a C program. See the
pcresample documentation for a short discussion.

   Compile thuswise:
   gcc -Wall pcredemo.c -I/usr/local/include -L/usr/local/lib /
   -R/usr/local/lib -lpcre
  
Replace "/usr/local/include" and "/usr/local/lib" with wherever the include and
library files for PCRE are installed on your system. Only some operating
systems (e.g. Solaris) use the -R option.
*/


#include <stdio.h>
#include <string.h>
#include <pcre.h>

#define OVECCOUNT 30     /* should be a multiple of 3 */


int main(int argc, char **argv)
{
pcre *re;
const char *error;
char *pattern;
char *subject;
unsigned char *name_table;
int erroffset;
int find_all;
int namecount;
int name_entry_size;
int ovector[OVECCOUNT];
int subject_length;
int rc, i;


/**************************************************************************
* First, sort out the command line. There is only one possible option at   *
* the moment, "-g" to request repeated matching to find all occurrences,   *
* like Perl's /g option. We set the variable find_all to a non-zero value *
* if the -g option is present. Apart from that, there must be exactly two *
* arguments.                                                               *
**************************************************************************/

find_all = 0;
for (i = 1; i < argc; i++) {
   if (strcmp(argv[i], "-g") == 0) find_all = 1;
   else break;
}

/* After the options, we require exactly two arguments, which are the pattern,
and the subject string. */

if (argc - i != 2) {
   printf("Two arguments required: a regex and a subject string/n");
   return 1;
}

pattern = argv[i];
subject = argv[i+1];
subject_length = (int)strlen(subject);


/*************************************************************************
* Now we are going to compile the regular expression pattern, and handle *
* and errors that are detected.                                           *
*************************************************************************/

re = pcre_compile(
   pattern,               /* the pattern */
   0,                     /* default options */
   &error,                /* for error message */
   &erroffset,            /* for error offset */
   NULL);                 /* use default character tables */


/* Compilation failed: print the error message and exit */

if (re == NULL) {
   printf("PCRE compilation failed at offset %d: %s/n", erroffset, error);
   return 1;
}


/*************************************************************************
* If the compilation succeeded, we call PCRE again, in order to do a      *
* pattern match against the subject string. This does just ONE match. If *
* further matching is needed, it will be done below.                      *
*************************************************************************/

rc = pcre_exec(
   re,                    /* the compiled pattern */
   NULL,                  /* no extra data - we didn't study the pattern */
   subject,               /* the subject string */
   subject_length,        /* the length of the subject */
   0,                     /* start at offset 0 in the subject */
   0,                     /* default options */
   ovector,               /* output vector for substring information */
   OVECCOUNT);            /* number of elements in the output vector */

/* Matching failed: handle error cases */

if (rc < 0) {
   switch(rc) {
    case PCRE_ERROR_NOMATCH: printf("No match/n"); break;
   /*
   Handle other special cases if you like
    */
    default: printf("Matching error %d/n", rc); break;
   }
   free(re);      /* Release memory used for the compiled pattern */
    return 1;
}

/* Match succeded */

printf("/nMatch succeeded at offset %d/n", ovector[0]);


/*************************************************************************
* We have found the first match within the subject string. If the output *
* vector wasn't big enough, set its size to the maximum. Then output any *
* substrings that were captured.                                          *
*************************************************************************/

/* The output vector wasn't big enough */

if (rc == 0) {
   rc = OVECCOUNT/3;
   printf("ovector only has room for %d captured substrings/n", rc - 1);
}

/* Show substrings stored in the output vector by number. Obviously, in a real
application you might want to do things other than print them. */

for (i = 0; i < rc; i++) {
   char *substring_start = subject + ovector[2*i];
   int substring_length = ovector[2*i+1] - ovector[2*i];
   printf("%2d: %.*s/n", i, substring_length, substring_start);
}


/**************************************************************************
* That concludes the basic part of this demonstration program. We have     *
* compiled a pattern, and performed a single match. The code that follows *
* first shows how to access named substrings, and then how to code for     *
* repeated matches on the same subject.                                    *
**************************************************************************/

/* See if there are any named substrings, and if so, show them by name. First
we have to extract the count of named parentheses from the pattern. */

(void)pcre_fullinfo(
   re,                    /* the compiled pattern */
   NULL,                  /* no extra data - we didn't study the pattern */
   PCRE_INFO_NAMECOUNT,   /* number of named substrings */
   &namecount);           /* where to put the answer */

if (namecount <= 0) printf("No named substrings/n"); else
{
   unsigned char *tabptr;
   printf("Named substrings/n");
  
   /* Before we can access the substrings, we must extract the table for
   translating names to numbers, and the size of each entry in the table. */
  
   (void)pcre_fullinfo(
    re,                        /* the compiled pattern */
    NULL,                      /* no extra data - we didn't study the pattern */
    PCRE_INFO_NAMETABLE,       /* address of the table */
    &name_table);              /* where to put the answer */
  
   (void)pcre_fullinfo(
    re,                        /* the compiled pattern */
    NULL,                      /* no extra data - we didn't study the pattern */
    PCRE_INFO_NAMEENTRYSIZE,   /* size of each entry in the table */
    &name_entry_size);         /* where to put the answer */
  
            /* Now we can scan the table and, for each entry, print the number, the name,
   and the substring itself. */
  
   tabptr = name_table;
   for (i = 0; i < namecount; i++)   {
    int n = (tabptr[0] << 8) | tabptr[1];
    printf("(%d) %*s: %.*s/n", n, name_entry_size - 3, tabptr + 2,
     ovector[2*n+1] - ovector[2*n], subject + ovector[2*n]);
    tabptr += name_entry_size;
   }
}


/*************************************************************************
* If the "-g" option was given on the command line, we want to continue   *
* to search for additional matches in the subject string, in a similar    *
* way to the /g option in Perl. This turns out to be trickier than you    *
* might think because of the possibility of matching an empty string.     *
* What happens is as follows:                                             *
*                                                                         *
* If the previous match was NOT for an empty string, we can just start    *
* the next match at the end of the previous one.                          *
*                                                                         *
* If the previous match WAS for an empty string, we can't do that, as it *
* would lead to an infinite loop. Instead, a special call of pcre_exec() *
* is made with the PCRE_NOTEMPTY and PCRE_ANCHORED flags set. The first   *
* of these tells PCRE that an empty string is not a valid match; other    *
* possibilities must be tried. The second flag restricts PCRE to one      *
* match attempt at the initial string position. If this match succeeds,   *
* an alternative to the empty string match has been found, and we can     *
* proceed round the loop.                                                 *
*************************************************************************/

if (!find_all) {
   free(re);    /* Release the memory used for the compiled pattern */
   return 0;    /* Finish unless -g was given */
}

/* Loop for second and subsequent matches */

for (;;) {
   int options = 0;                  /* Normally no options */
   int start_offset = ovector[1];    /* Start at end of previous match */
  
            /* If the previous match was for an empty string, we are finished if we are
            at the end of the subject. Otherwise, arrange to run another match at the
   same point to see if a non-empty match can be found. */
  
   if (ovector[0] == ovector[1])   {
    if (ovector[0] == subject_length) break;
    options = PCRE_NOTEMPTY | PCRE_ANCHORED;
   }
  
   /* Run the next matching operation */
  
   rc = pcre_exec(
    re,                    /* the compiled pattern */
    NULL,                  /* no extra data - we didn't study the pattern */
    subject,               /* the subject string */
    subject_length,        /* the length of the subject */
    start_offset,          /* starting offset in the subject */
    options,               /* options */
    ovector,               /* output vector for substring information */
    OVECCOUNT);            /* number of elements in the output vector */
  
           /* This time, a result of NOMATCH isn't an error. If the value in "options"
           is zero, it just means we have found all possible matches, so the loop ends.
           Otherwise, it means we have failed to find a non-empty-string match at a
           point where there was a previous empty-string match. In this case, we do what
           Perl does: advance the matching position by one, and continue. We do this by
           setting the "end of previous match" offset, because that is picked up at the
   top of the loop as the point at which to start again. */
  
   if (rc == PCRE_ERROR_NOMATCH)   {
    if (options == 0) break;
    ovector[1] = start_offset + 1;
    continue;     /* Go round the loop again */
   }
  
   /* Other matching errors are not recoverable. */
  
   if (rc < 0)   {
    printf("Matching error %d/n", rc);
    free(re);     /* Release memory used for the compiled pattern */
    return 1;
   }
  
   /* Match succeded */
  
   printf("/nMatch succeeded again at offset %d/n", ovector[0]);
  
   /* The match succeeded, but the output vector wasn't big enough. */
  
   if (rc == 0)   {
    rc = OVECCOUNT/3;
    printf("ovector only has room for %d captured substrings/n", rc - 1);
   }
  
  
   /* As before, show substrings stored in the output vector by number, and then
   also any named substrings. */
  
   for (i = 0; i < rc; i++)   {
    char *substring_start = subject + ovector[2*i];
    int substring_length = ovector[2*i+1] - ovector[2*i];
    printf("%2d: %.*s/n", i, substring_length, substring_start);
   }
  
   if (namecount <= 0) printf("No named substrings/n"); else
   {
    unsigned char *tabptr = name_table;
    printf("Named substrings/n");
    for (i = 0; i < namecount; i++)
    {
     int n = (tabptr[0] << 8) | tabptr[1];
     printf("(%d) %*s: %.*s/n", n, name_entry_size - 3, tabptr + 2,
      ovector[2*n+1] - ovector[2*n], subject + ovector[2*n]);
     tabptr += name_entry_size;
    }
   }
}       /* End of loop to find second and subsequent matches */

printf("/n");
free(re);        /* Release memory used for the compiled pattern */
return 0;
}

/* End of pcredemo.c */

转自:http://www.cublog.cn/u1/33888/showart_341825.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【为什么还需要学习C++?】 你是否接触很多语言,但从来没有了解过编程语言的本质?你是否想成为一名资深开发人员,想开发别人做不了的高性能程序?你是否经常想要窥探大型企业级开发工程的思路,但苦于没有基础只能望洋兴叹? 那么C++就是你个人能力提升,职业之路进阶的不二之选。【课程特色】 1.课程共19大章节,239课时内容,涵盖数据结构、函数、类、指针、标准库全部知识体系。2.带你从知识与思想的层面从0构建C++知识框架,分析大型项目实践思路,为你打下坚实的基础。3.李宁老师结合4大国外顶级C++著作的精华为大家推出的《征服C++11》课程。【学完后我将达到什么水平?】 1.对C++的各个知识能够熟练配置、开发、部署;2.吊打一切关于C++的笔试面试题;3.面向物联网的“嵌入式”和面向大型化的“分布式”开发,掌握职业钥匙,把握行业先机。【面向人群】 1.希望一站式快速入门的C++初学者; 2.希望快速学习 C++、掌握编程要义、修炼内功的开发者; 3.有志于挑战更高级的开发项目,成为资深开发的工程师。 【课程设计】 本课程包含3大模块基础篇本篇主要讲解c++的基础概念,包含数据类型、运算符等基本语法,数组、指针、字符串等基本词法,循环、函数、类等基本句法等。进阶篇本篇主要讲解编程中常用的一些技能,包含类的高级技术、类的继承、编译链接和命名空间等。提升篇:本篇可以帮助学员更加高效的进行c++开发,其中包含类型转换、文件操作、异常处理、代码重用等内容。
### 回答1: 要使用正则表达式,您需要使用一些语法来表示要匹配的文本模式。以下是一些基本的正则表达式语法: 1. 字符匹配 - 用方括号 [] 包括一组字符,表示在这些字符中匹配任意一个字符。例如 [abc] 匹配 a、b 或 c。 - 用横杠 - 表示一个字符范围。例如 [a-z] 匹配任何小写字母。 - 用反斜杠 \ 来转义特殊字符。例如,如果想匹配小数点(.),需要写成 \.。 2. 量词匹配 - 用星号 * 表示匹配前面的字符 0 次或多次。例如 a* 可以匹配 a、aa、aaa 等。 - 用加号 + 表示匹配前面的字符 1 次或多次。例如 a+ 可以匹配 a、aa、aaa 等。 - 用问号 ? 表示匹配前面的字符 0 次或 1 次。例如 a? 可以匹配 a 或空字符串。 3. 分组和捕获 - 用圆括号 () 表示一个分组,可以对分组内的内容进行后续处理。例如 (ab)+ 表示匹配一个或多个连续的 ab。 - 用竖杠 | 表示或者。例如 (a|b) 匹配 a 或 b。 这只是正则表达式语法的基础,正则表达式非常强大且复杂,需要深入学习才能掌握。 ### 回答2: 正则表达式是一种强大的文本匹配工具,可以用于快速、准确地查找和处理字符串。以下是使用正则表达式的一般步骤: 1. 导入正则表达式模块:在使用正则表达式之前,需要先导入相应的模块,比如Python中的re模块。 2. 创建正则表达式:使用合适的语法创建一个符合需求的正则表达式正则表达式由特定字符和符号组成,用于定义某种模式。 3. 创建匹配对象:使用re模块中的函数,如re.compile()编译正则表达式,创建一个匹配对象。这个对象可以根据正则表达式来查找和处理字符串。 4. 匹配模式:利用匹配对象调用相应的方法,如match()、search()、findall()和sub(),根据不同的需求进行模式匹配。 5. 匹配结果处理:根据具体需求,对匹配结果进行处理。可以提取出匹配的字符串、替换指定的字符、或者对匹配结果进行其他操作。 6. 循环匹配:根据实际情况,可以对一个字符串进行多次匹配。可以使用while循环、for循环等方式。 7. 输出或使用:根据需求选择将匹配结果输出到控制台、存入文件,或者在程序中继续使用。 通过上述步骤,我们可以灵活运用正则表达式来查找和处理符合特定模式的字符串。但是需要注意,正则表达式的语法较为繁琐,需要一定的学习和练习。同时,复杂的正则表达式可能会影响程序的性能,因此在使用时需要进行优化和测试。 ### 回答3: 使用正则表达式可以通过以下几个步骤: 1. 导入正则表达式模块:在编程语言中,一般都有支持正则表达式的库或模块,需要先导入相应的模块。 2. 创建正则表达式正则表达式是由一系列字符和特殊字符组成的模式,用于匹配字符串。可以使用特殊字符、元字符和字符类来定义自己需要的模式。 3. 编译正则表达式:将创建的正则表达式编译成一个可供计算机识别和处理的对象。这样可以提高匹配的效率。 4. 使用正则表达式进行匹配:使用编译好的正则表达式对象对目标字符串进行匹配。可以使用匹配函数或方法对字符串进行匹配操作,返回匹配到的结果。 5. 获取匹配结果:匹配函数或方法会返回匹配的结果,可以根据需要进行进一步的处理和操作。可以获取匹配的子串、位置或其他相关信息。 6. 重复匹配:正则表达式通常可以进行多次重复匹配。可以使用循环等方式对目标字符串进行重复匹配,找到所有符合要求的子串。 7. 替换和修改:正则表达式还可以用来进行替换和修改操作。可以使用替换函数或方法将匹配到的内容进行替换,或者通过修改正则表达式的模式来达到修改字符串的目的。 需要注意的是,正则表达式的语法和使用方法会因不同的编程语言而有所差异,具体使用时需要参考相应的文档和教程。同时,正则表达式的匹配和使用也需要一定的经验和技巧,对于复杂的匹配需要深入学习和实践。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值