因为自己写的正则死活匹配不了,就搜了半天 c 的正则怎么写,发现别人的也不匹配。
下面举例的代码是网上别人的: https://ideone.com/TH5t3U
#include <stdio.h>
#include <regex.h>
#include <stdlib.h>
#define REGEX "prefix:\\w+,\\w+,\\s*-?[0-9]{1,4}\\s*,\\s*-?[0-9]{1,4}\\s*,\\s*-?[0-9]{1,4}\\s*,\\w*"
const char *input = "prefix:string,string,-100,100,0,string";
int main(){
int rc;
regex_t regex;
rc = regcomp(®ex, REGEX, REG_EXTENDED);
if (rc != 0) {
fprintf(stderr, "Could not compile regex\n");
exit(1);
}
rc = regexec(®ex, input, 0, NULL, 0);
if (rc == 0) {
printf("Match!\n");
return 0;
}
else if (rc == REG_NOMATCH) {
printf("No match\n");
return -1;
}
else {
perror("Error\n");
exit(1);
}
return 0;
}
还以为自己傻了,折腾了半天,到后来发现这个正则我在 arm linux 上,无论是 gcc 还是 clang 编译结果都能匹配。