加入星计划,您可以享受以下权益:

  • 创作内容快速变现
  • 行业影响力扩散
  • 作品版权保护
  • 300W+ 专业用户
  • 1.5W+ 优质创作者
  • 5000+ 长期合作伙伴
立即加入
  • 正文
  • 推荐器件
  • 相关推荐
  • 电子产业图谱
申请入驻 产业图谱

2 个小例子了解 C 语言使用正则表达式

2023/04/20
2135
阅读需 7 分钟
加入交流群
扫码加入
获取工程师必备礼包
参与热点资讯讨论

哈喽,我是老吴。我的日常工作基本离不开正则表达式,不过一般是在 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(&regex, re, REG_NEWLINE))
       exit(EXIT_FAILURE);

   printf("Matches:n");
   for (int i = 0; ; i++) {
       if (regexec(&regex, 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]orld\s*[!]?");

    /* 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 ——

推荐器件

更多器件
器件型号 数量 器件厂商 器件描述 数据手册 ECAD模型 风险等级 参考价格 更多信息
DS24B33S+T&R 1 Maxim Integrated Products EEPROM, 4KX1, Serial, CMOS, PDSO8, 0.208 INCH, ROHS COMPLIANT, SOP-8
$2.75 查看
ABS07-LR-32.768KHZ-6-1-T 1 Abracon Corporation CRYSTAL 32.768KHZ 6PF SMD

ECAD模型

下载ECAD模型
$2.7 查看
DS2431P-A1+T 1 Maxim Integrated Products EEPROM, 1KX1, Serial, CMOS, PDSO6, ROHS COMPLIANT, TSOC-6
$3.23 查看

相关推荐

电子产业图谱