在C/C++语言中使用正则表达式
本文所有的内容都来自网络,加上一些个人的理解,不保证正确性,欢迎批评指正
说起正则表达式(Regular Expression),也许有的朋友天天都在使用,比如grep、vim、sed、awk,只是可能对这个名词不大熟悉。正则表达式一般简写为regex或者regexp,甚至是RE。关于正则表达式的介绍,有很多的文章,用搜索引擎查找就可以找到很不错的使用说明。但是在C/C++语言中如何去使用,相应的介绍比较缺乏。大多数C标准库自带regex,可以通过/usr/include/regex.h去看,或者man regex看使用说明。perl,php等语言更是提供了功能强大的正则表达式,最著名的C语言正则表达式库为PCRE(Perl Compatible Regular Expression)。本文主要对regex和pcre的使用做一点入门介绍。
1、regex
regex的使用非常简单,只要看一下示例代码1就能明白(示例代码是从“GNU C 规则表达式入门”这篇文章里摘取出来的,是否为原始出处就
不得而知了)。
CODE:#include <stdio.h>
#include <string.h>
#include <regex.h>
#define SUBSLEN 10 /* 匹配子串的数量 */
#define EBUFLEN 128 /* 错误消息buffer长度 */
#define BUFLEN 1024 /* 匹配到的字符串buffer长度 */
int main()
{
size_t len;
regex_t re; /* 存储编译好的正则表达式,正则表达式在使用之前要经过编译 */
regmatch_t subs [SUBSLEN]; /* 存储匹配到的字符串位置 */
char matched [BUFLEN]; /* 存储匹配到的字符串 */
char errbuf [EBUFLEN]; /* 存储错误消息 */
int err, i;
char src [] = "111 <title>Hello World</title> 222"; /* 源字符串 */
char pattern [] = "<title>(.*)</title>"; /* pattern字符串 */
printf("String : %s\n", src);
printf("Pattern: \"%s\"\n", pattern);
/* 编译正则表达式 */
err = regcomp(&re, pattern, REG_EXTENDED);
if (err) {
len = regerror(err, &re, errbuf, sizeof(errbuf));
printf("error: regcomp: %s\n", errbuf);
return 1;
}
printf("Total has subexpression: %d\n", re.re_nsub);
/* 执行模式匹配 */
err = regexec(&re, src, (size_t) SUBSLEN, subs, 0);
if (err == REG_NOMATCH) { /* 没有匹配成功 */
printf("Sorry, no match ...\n");
regfree(&re);
return 0;
} else if (err) { /* 其它错误 */
len = regerror(err, &re, errbuf, sizeof(errbuf));
printf("error: regexec: %s\n", errbuf);
return 1;
}
/* 如果不是REG_NOMATCH并且没有其它错误,则模式匹配上 */
printf("\nOK, has matched ...\n\n");
for (i = 0; i <= re.re_nsub; i++) {
len = subs[i].rm_eo - subs[i].rm_so;
if (i == 0) {
printf ("begin: %d, len = %d ", subs[i].rm_so, len); /* 注释1 */
} else {
printf("subexpression %d begin: %d, len = %d ", i, subs[i].rm_so, len);
}
memcpy (matched, src + subs[i].rm_so, len);
matched[len] = '\0';
printf("match: %s\n", matched);
}
regfree(&re); /* 用完了别忘了释放 */
return (0);
}
执行结果是
CODE:String : 111 <title>Hello World</title> 222
Pattern: "<title>(.*)</title>"
Total has subexpression: 1
OK, has matched ...
begin: %, len = 4 match: <title>Hello World</title>
subexpression 1 begin: 11, len = 11 match: Hello World
从示例程序可以看出,使用之前先用regcomp()编译一下,然后调用regexec()进行实际匹配。如果只是看有没有匹配成功,掌握这2个函数的用法即可。有时候我们想要取得匹配后的子表达式,比如示例中想获得title是什么,需要用小括号 "( )"把子表达式括起来"<title>(.*)</title>",表达式引擎会将小括号 "( )" 包含的表达式所匹配到的字符串记录下来。在获取匹配结果的时候,小括号包含的表达式所匹配到
的字符串可以单独获取,示例程序就是我用来获取http网页的主题(title)的方式。
regmatch_t subs[SUBSLEN]是用来存放匹配位置的,subs[0]里存放这个匹配的字符串位置,subs[1]里存放第一个子表达式的匹配位置,也就是例子中的title,通过结构里的rm_so和rm_eo可以取到,这一点很多人不太注意,应该强调一下。
注释1:开始调试代码的时候是在FreeBSD 6.2上进行的,print出来的len总是0,但print出来的字符串又没错,很是迷惑,把它放到Linux上则完全正常,后来仔细检查才发现rm_so在Linux上是32位,在FreeBSD上是64位,用%d的话实际取的是rm_so的高32位,而不是实际的len,把print rm_so的地方改为%llu就可以了。
regex虽然简单易用,但对正则表达式的支持不够强大,中文处理也有问题,于是引出了下面要说的PCRE。
2、PCRE (http://www.pcre.org)
PCRE的名字就说明了是Perl Compatible,熟悉Perl、PHP的人使用起来完全没有问题。PCRE有非常丰富的使用说明和示例代码(看看
pcredemo.c就能明白基本的用法),下面的程序只是把上面regex改为pcre。
CODE:/* Compile thuswise:
* gcc -Wall pcre1.c -I/usr/local/include -L/usr/local/lib -R/usr/local/lib -lpcre
*
*/
#include <stdio.h>
#include <string.h>
#include <pcre.h>
#define OVECCOUNT 30 /* should be a multiple of 3 */
#define EBUFLEN 128
#define BUFLEN 1024
int main()
{
pcre *re;
const char *error;
int erroffset;
int ovector[OVECCOUNT];
int rc, i;
char src [] = "111 <title>Hello World</title> 222";
char pattern [] = "<title>(.*)</title>";
printf("String : %s\n", src);
printf("Pattern: \"%s\"\n", pattern);
re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
if (re == NULL) {
printf("PCRE compilation failed at offset %d: %s\n", erroffset, error);
return 1;
}
rc = pcre_exec(re, NULL, src, strlen(src), 0, 0, ovector, OVECCOUNT);
if (rc < 0) {
if (rc == PCRE_ERROR_NOMATCH) printf("Sorry, no match ...\n");
else printf("Matching error %d\n", rc);
free(re);
return 1;
}
printf("\nOK, has matched ...\n\n");
for (i = 0; i < rc; i++) {
char *substring_start = src + ovector[2*i];
int substring_length = ovector[2*i+1] - ovector[2*i];
printf("%2d: %.*s\n", i, substring_length, substring_start);
}
free(re);
return 0;
}
执行结果是:
CODE:String : 111 <title>Hello World</title> 222
Pattern: "<title>(.*)</title>"
OK, has matched ...
0: <title>Hello World</title>
1: Hello World
比较这2个例子可以看出,在regex用的是regcomp()、regexec(),pcre则使用pcre_compile()、pcre_exec(),用法几乎完全一致。
pcre_compile()有很多选项,详细说明参见http://www.pcre.org/pcre.txt。如果是多行文本,可以设置PCRE_DOTALL的选项pcre_complie(re,
PCRE_DOTALL,....),表示'.'也匹配回车换行"\r\n"。
3、pcre++
pcre++(http://www.daemon.de/PCRE)对pcre做了c++封装,使用起来更加方便。
CODE:/*
* g++ pcre2.cpp -I/usr/local/include -L/usr/local/lib -R/usr/local/lib -lpcre++ -lpcre
*/
#include <string>
#include <iostream>
#include <pcre++.h>
using namespace std;
using namespace pcrepp;
int main()
{
string src("111 <title>Hello World</title> 222");
string pattern("<title>(.*)</title>");
cout << "String : " << src << endl;
cout << "Pattern : " << pattern << endl;
Pcre reg(pattern, PCRE_DOTALL);
if (reg.search(src) == true) { //
cout << "\nOK, has matched ...\n\n";
for(int pos = 0; pos < reg.matches(); pos++) {
cout << pos << ": " << reg[pos] << endl;
}
} else {
cout << "Sorry, no match ...\n";
return 1;
}
return 0;
}
执行结果是:
CODE:String : 111 <title>Hello World</title> 222
Pattern : <title>(.*)</title>
OK, has matched ...
0: Hello World
4、oniguruma
还有一个正则表达式的库oniguruma(http://www.geocities.jp/kosako3/oniguruma/),对于东亚文字支持比较好,开始是用在ruby上,也可用于C++,是日本的开发人员编写的。大多数人都不会用到,也就不做介绍了。如果有疑问可以通过email来讨论它的用法。
5、Regular Expression的内部实现
关于Regular Expression的实现,用到了不少自动机理论(Automata Theory)的知识,有兴趣的可以找这方面的资料来看,这本书“
Introduction to Automata Theory, Languages, and Computation”写的很好,编译原理的书也有这方面的内容。
相关文章

网络工程师必备的6款配置文件编辑神器:替代Notepad++!
本期文章介绍了六种网工必备工具,这些工具是网络工程师日常工作中必不可少的,也是那些立志成为网络工程师的人应该熟悉的2026-04-13
首个版本v1.111已上线! VS Code开启周更狂飙模式
026年3月10日,微软宣布将Visual Studio Code(VS Code)更新节奏从月更提速至周更,并发布首个周更版本v1.1112026-03-11
还在手动打开浏览器、点击按钮、检查控制台报错?太累了!现在,最新的VS Code 1.110中AI可以直接调试浏览器2026-03-10
今天带大家来共同学习VC++6.0无法正常运行怎么办?下面就是详细完整的操作步骤,快跟小编一起来学习一下2026-02-27
vscode调试c代码始终找不到exe可执行文件的详细及解决办法
在 VS Code 中调试 C++ 项目时,常见报错:“无法启动调试会话:找不到可执行文件”,这个问题问题通常是由于环境配置不当、缺少编译步骤、路径设置错误、或是launch.json2026-02-27
vscode入门! 小白都能看懂的VS Code保姆级级使用教程
相对于其它前端工具来说,VSCode显得更加的流畅,今天就将我的学习经历和大家分享一下,希望可以帮助到有需要的人2026-02-27
2026年真正值得装、用得上、不鸡肋的 VSCode 插件,其实没那么多——挑10个“神级”不难,但得是每天打开就用、关掉就难受的那种2026-02-10
VS Code 和 VSCodium怎么选? vscode 和 vscodium之间的区别
咱们今天就来聊聊 Visual Studio Code (VS Code) 和 VSCodium的相似与区别,详细请看下文介绍2026-01-10
如何启用.NET Framework3.5? 一键安装net3.5解决软件依赖问题
你是否曾在安装某些软件时遇到过“需要.NET Framework 3.5”的提示?实,只需要几步简单的操作,你就可以轻松解决这个问题2025-10-28
在软件开发过程中,重构是一个非常重要的环节,重构可以提高代码的可读性、可维护性和可扩展性,使代码更加健壮和易于理解,本文将介绍如何使用 VSCode 进行 Java 代码重构2025-09-20






最新评论