【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.
相关推荐
菜鸟赵大宝4 分钟前
Visual Studio编译优化选项
c++·visual studio
北岛寒沫7 分钟前
算法刷题笔记 单调栈(C++实现)
c++·笔记·算法
一条闲鱼。9 分钟前
C++ 文件读写
c++·读写文件
imred11 分钟前
C++:std::function的libc++实现
c++·stl·template·function·libc++
阿猿收手吧!15 分钟前
【Linux】TCP协议【下二】{流量控制/滑动窗口/延迟应答/捎带应答/拥塞控制}
linux·网络·网络协议·tcp/ip
jzwalliser16 分钟前
洛谷 P1035 [NOIP2002 普及组] 级数求和
c++·python·算法
Saniana25 分钟前
Hive On Spark语法
linux·hive·hadoop·spark
汀小烟38 分钟前
使用静态成员函数统计学生平均成绩
c语言·开发语言·数据结构·c++·vscode
我要成为C++领域大神40 分钟前
【高性能服务器】select模型
linux·服务器·c语言·开发语言·网络·tcp·io多路复用
bitcsljl1 小时前
Linux系统中卸载GitLab
linux·运维·gitlab