我一直在尝试在我正在开发的C项目中使用正则表达式(<regex.h>
)。
根据regex101的正则表达式,它写得很好,并确定了我想要识别的东西,但当我试图在c中运行它时,它不起作用。
#include <stdio.h>
#include <regex.h>
int main() {
char pattern[] = "#include.*";
char line[] = "#include <stdio.h>";
regex_t string;
int regex_return = -1;
regex_return = regcomp(&string, line, 0);
regex_return += regexec(&string, pattern, 0, NULL, 0);
printf("%d", regex_return);
return 0;
}
这是一个示例代码,当我发现表达式不工作时,我写了一个测试。
- 它打印1,当我期望0。
- 如果我将
line
更改为"#include"
,它打印0,这对我来说很奇怪,因为它忽略了最后的.*
。
line
和pattern
被交换。
regcomp
取模式,regexec
取字符串。