1. 优秀
2. 简单使用
分四步:
regcomp/regexec/regfree
#include <regex.h>
int dregex(char* parttern, char* src) {
int ret;
int result = 1;
regmatch_t rgm;
regex_t reg;
ret = regcomp(®, parttern, REG_NOSUB | REG_EXTENDED);
if (ret != 0) {
return -1;
}
ret = regexec(®, src, 1, &rgm, REG_NOTBOL);
if (ret == 0) { // REG_NOERROR 匹配成功,找到不合规
result = 0;
} else if (ret == 1) { // REG_NOMATCH 未找到符合规则字符串
result = 1;
} else {
result = -1;
}
regfree(®);
return result;
}