【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.
相关推荐
筑梦之路19 分钟前
linux 系统找出磁盘IO占用元凶 —— 筑梦之路
linux·运维·服务器
zyx没烦恼1 小时前
五种IO模型
开发语言·c++
LiQiang332 小时前
Ubuntu2404修改国内镜像
linux
杰哥技术分享2 小时前
Ubuntu 22.04安装SQL Server指南
linux·运维·ubuntu·sqlserver
遇见火星2 小时前
ubuntu18.04 升级Ubuntu 20.04
linux·运维·ubuntu·系统升级
EutoCool2 小时前
Qt窗口:菜单栏
开发语言·c++·嵌入式硬件·qt·前端框架
x县豆瓣酱2 小时前
【第四节】ubuntu server安装docker
linux·ubuntu·docker
Gene_20222 小时前
【TOOL】ubuntu升级cmake版本
linux·运维·ubuntu
思序 LogicFlow2 小时前
关于在Linux上部署 SecretFlow --- P2P部署模式
linux·服务器
YC运维3 小时前
网络配置综合实验全攻略(对之前学习的总结)
linux·服务器·网络