哈喽,我是老吴。我的日常工作基本离不开正则表达式,不过一般是在 Shell 脚本或者命令行里使用。今天抽空了解了一下 C 语言下的使用方式,马上分享给大家。如果你还不了解正则表达式,可以先瞅一眼 《Linux Shell 脚本攻略》第 4.2 章节正则表达式入门。
五分钟就可以入门,下面来看第一个例子。
#include <regex.h>
static const char *const str =
"1) John Driverhacker;n2) John Doe;n3) John Foo;n";
static const char *const re = "John.*o";
int main(void)
{
static const char *s = str;
regex_t regex;
regmatch_t pmatch[1];
regoff_t off, len;
if (regcomp(®ex, re, REG_NEWLINE))
exit(EXIT_FAILURE);
printf("Matches:n");
for (int i = 0; ; i++) {
if (regexec(®ex, s, ARRAY_SIZE(pmatch), pmatch, 0))
break;
off = pmatch[0].rm_so + (s - str);
len = pmatch[0].rm_eo - pmatch[0].rm_so;
printf("#%d:n", i);
printf("offset = %jd; length = %jdn", (intmax_t) off,
(intmax_t) len);
printf("substring = "%.*s"n", len, s + pmatch[0].rm_so);
s += pmatch[0].rm_eo;
}
}
运行结果:
$ ./example Matches: #0: offset = 25; length = 7 substring = "John Do" #1: offset = 38; length = 8 substring = "John Foo"
成功匹配出符合 "John.*o" 表达式的两个字符串。
一般正则表达式相关的接口就是 2 个:1、编译,对应 regcomp();2、查找匹配项,对应 regexec();匹配到的结果会保存在结构体 regmatch_t 里,注意,这个结构体里只保存了匹配项的 start offset 和 end offset。
typedef struct {
regoff_t rm_so;
regoff_t rm_eo;
} regmatch_t;
如果你的系统不是 Linux,而是嵌入式 RTOS 或者裸机程序,可以使用这个开源的正则表达式库:
https://github.com/kokke/tiny-regex-c
tiny-regex-c 是一个有 1K star 的超小巧 C 语言 regex 库。源码就 2 个文件:re.c 和 re.h,超容易移植。用法看这个例子:
#include "re.h"
int main(void)
{
/* Standard int to hold length of match */
int match_length;
/* Standard null-terminated C-string to search: */
const char* string_to_search = "ahem.. 'hello world !' ..";
/* Compile a simple regular expression using character classes, meta-char and greedy + non-greedy quantifiers: */
re_t pattern = re_compile("[Hh]ello [Ww]orlds*[!]?");
/* Check if the regex matches the text: */
int match_idx = re_matchp(pattern, string_to_search, &match_length);
if (match_idx != -1) {
printf("match: %sn", string_to_search + match_idx);
}
}
运行结果:
match: hello world !' ..
tiny-regex-c 的 接口和 第一个例子 的posix regex 类似,都是一目了然,就不再赘述了。
—— The End ——
阅读全文
758