正则表达式

2020-04-21  本文已影响0人  如果听见下雨的声音
#include <iostream>
#include <regex.h>

void testRegex()
{
    const char str[] = "sdfsijweu34.67.5.89werqwesdfuji192.168.1.132sdfksdfjkghjisdf";

    const char regex[] = "([0-9]{1,3}\\.){3,3}[0-9]{1,3}";

    struct re_pattern_buffer preg{};

    int ret;

    ret = regcomp(&preg, regex, REG_EXTENDED);

    if (ret < 0)
    {
        char errbuf[BUFSIZ];

        regerror(ret, &preg, errbuf, sizeof(errbuf));

        write(STDOUT_FILENO, errbuf, strlen(errbuf));

        exit(ret);
    }

    const size_t size = 256;

    regmatch_t pmatch[size];

    ret = regexec(&preg, str, size, pmatch, 0);

    for (auto &index : pmatch)
    {
        write(STDOUT_FILENO, str + index.rm_so, index.rm_eo - index.rm_so - 1);
    }

    regfree(&preg);

}

上一篇下一篇

猜你喜欢

热点阅读