【POSIX】使用regex进行正则匹配

正则表达式是很关键的一个工具,各种编程语言中均通用,务必掌握

给出Linux中man page给出的一个示例:

cpp 复制代码
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <regex.h>

#define ARRAY_SIZE(arr) (sizeof((arr)) / sizeof((arr)[0]))

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("String = \"%s\"\n", str);
    printf("Matches:\n");

    for (unsigned 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("#%zu:\n", i);
        printf("offset = %jd; length = %jd\n", (intmax_t)off,
               (intmax_t)len);
        printf("substring = \"%.*s\"\n", len, s + pmatch[0].rm_so);

        s += pmatch[0].rm_eo;
    }

    exit(EXIT_SUCCESS);
}

执行:

cpp 复制代码
./demo           
String = "1) John Driverhacker;
2) John Doe;
3) John Foo;
"
Matches:
#0:
offset = 25; length = 7
substring = "John Do"
#1:
offset = 38; length = 8
substring = "John Foo"

然后依样画葫芦,也写一个动态生成正则匹配的工具

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>
#include <regex.h>

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        std::cerr << "./regexec <reg-str>" << std::endl;
        return 0;
    }

    std::cout << "reg pattern: " << argv[1] << std::endl;

    char msgbuf[128];
    regex_t r{};
    int err = regcomp(&r, argv[1], REG_NEWLINE);
    if (err != 0)
    {
        std::cout << "err code: " << err << std::endl;
        size_t s = regerror(err, &r, msgbuf, sizeof(msgbuf));
        write(STDERR_FILENO, msgbuf, s);
        exit(-1);
    }

    std::string str;
    for (;;)
    {
        std::cout << "请输入待匹配的字符串: ";
        std::getline(std::cin, str);
        std::cout << "输入是: " << str.c_str() << std::endl;

        int want_len = 1;
        regmatch_t want[1];

        err = regexec(&r, str.c_str(), want_len, want, 0);
        if (err == 0)
        {
            printf("The string matches the regular expression.\n");
        }
        else if (err == REG_NOMATCH)
        {
            printf("The string does not match the regular expression.\n");
        }
        else
        {
            regerror(err, &r, msgbuf, sizeof(msgbuf));
            fprintf(stderr, "Regex matching failed: %s\n", msgbuf);
            exit(-1);
        }
    }

    regfree(&r);

    return 0;
}

执行:

cpp 复制代码
./regexec "[0-9].*" 
reg pattern: [0-9].*
请输入待匹配的字符串: 123
输入是: 123
The string matches the regular expression.
请输入待匹配的字符串: 12
输入是: 12
The string matches the regular expression.
请输入待匹配的字符串: addd
输入是: addd
The string does not match the regular expression.
请输入待匹配的字符串: 12sfsfs
输入是: 12sfsfs
The string matches the regular expression.
请输入待匹配的字符串: sdfs
输入是: sdfs
The string does not match the regular expression.
相关推荐
wregjru1 分钟前
【读书笔记】Effective C++ 条款1~2 核心编程准则
java·开发语言·c++
秋4271 小时前
防火墙基本介绍与使用
linux·网络协议·安全·网络安全·架构·系统安全
取加若则_1 小时前
深入解析Linux进程优先级机制
linux·服务器
点亮一颗LED(从入门到放弃)1 小时前
设备模型(10)
linux·服务器·前端
Web极客码1 小时前
使用VPS主机进行数据分析的主要优势
linux·windows·vps主机
青岛少儿编程-王老师1 小时前
CCF编程能力等级认证GESP—C++1级—20251227
java·c++·算法
九思x1 小时前
Linux 系统安装 JDK 17
linux·运维
微露清风2 小时前
系统性学习C++进阶-第十四讲-二叉搜索树
开发语言·c++·学习
HIT_Weston2 小时前
77、【Ubuntu】【Hugo】搭建私人博客:Detached HEAD
linux·运维·ubuntu
再睡一夏就好2 小时前
多线程并发编程核心:互斥与同步的深度解析及生产者消费者模型两种实现
linux·运维·服务器·jvm·c++·笔记