【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.
相关推荐
RussellFans5 分钟前
Linux 文本三剑客(grep, awk, sed)
linux·运维·服务器
PingdiGuo_guo16 分钟前
C++智能指针的知识!
开发语言·c++
Chuncheng's blog19 分钟前
CentOS 7如何编译安装升级gcc至7.5版本?
linux·运维·c++·centos
听风吹等浪起20 分钟前
CentOS在vmware局域网内搭建DHCP服务器【踩坑记录】
linux·服务器·centos
明月看潮生27 分钟前
青少年编程与数学 01-011 系统软件简介 04 Linux操作系统
linux·青少年编程·操作系统·系统软件·编程与数学
aitav01 小时前
⚡️ Linux Docker 基本命令参数详解
linux·运维·docker
姓刘的哦2 小时前
ubuntu中使用docker
linux·ubuntu·docker
代码程序猿RIP2 小时前
【Linux】(1)—进程概念-⑤进程调度
linux·运维
_lizhiqiang2 小时前
联想拯救者R9000P 网卡 Realtek 8852CE Ubuntu/Mint linux 系统睡眠后,无线网卡失效
linux·运维·ubuntu·r9000p·无线网卡·8852ce
愚润求学2 小时前
【C++】类型转换
开发语言·c++