#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);
}
网友评论