#include <stdio.h> #include <stdlib.h> #include <string.h> #include <pcre.h> int main() { const char *input_string = "(15:00:32 2023 user.info root:wificommon(repacd):guest password new_value:12345678,old_value:12345678)"; const char *pattern = "new_value:(\\d+),old_value:(\\d+)"; int ovector[30]; // 存放匹配位置的数组 pcre *re; const char *error; int erroffset; int rc; re = pcre_compile(pattern, 0, &error, &erroffset, NULL); if (re == NULL) { printf("编译正则表达式失败: %s\n", error); return 1; } rc = pcre_exec(re, NULL, input_string, strlen(input_string), 0, 0, ovector, sizeof(ovector)/sizeof(int)); if (rc < 0) { printf("正则匹配失败\n"); pcre_free(re); return 1; } // 获取匹配到的字段 char *new_value = NULL; char *old_value = NULL; pcre_get_substring(input_string, ovector, rc, 1, (const char **)&new_value); pcre_get_substring(input_string, ovector, rc, 2, (const char **)&old_value); printf("new_value: %s\n", new_value); printf("old_value: %s\n", old_value); pcre_free(re); pcre_free_substring(new_value); pcre_free_substring(old_value); return 0; }