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

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

给出Linuxman page给出的一个示例:

#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);
}

执行:

./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"

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

#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;
}

执行:

./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.

上一篇:使用llama-index连接neo4j知识图谱 达成大模型构建查询知识图谱功能


下一篇:PHP函数大全之array_count_values