-1
Or运算符(|)不能用C语言正则表达式,它总是给出匹配的输出,如果我给不正确的输入也是“12”或“ 123“或c显示为MATCH。我会请求在这种情况下提供帮助。| (或者运算符在C语言的正则表达式中不起作用)
#include <sys/types.h>
#include <regex.h>
#include <stdio.h>
int main(int argc, char *argv[]){ regex_t regex;
int reti;
char msgbuf[100];
/* Compile regular expression */
reti = regcomp(®ex, "1 | 2c | 3c", REG_EXTENDED);
if(reti){ fprintf(stderr, "Could not compile regex\n"); return(1); }
/* Execute regular expression */
reti = regexec(®ex, "123", 0, NULL, 0);
if(!reti){
puts("Match");
}
else if(reti == REG_NOMATCH){
puts("No match");
}
else{
regerror(reti, ®ex, msgbuf, sizeof(msgbuf));
fprintf(stderr, "Regex match failed: %s\n", msgbuf);
return 1;
}
/* Free compiled regular expression if you want to use the regex_t again */
regfree(®ex);
return 0;
}
没有标准的“C语言正则表达式”。你在使用哪个库? –
小心正则表达式中的空格,正则表达式中的所有字符都很重要。您可能还需要对子表达式进行分组,因为正则表达式不具有任何种类的运算符优先级。 –
[不匹配](“http://ideone.com/plXXVZ)”和“123”。用“2c”,[有一个匹配](http://ideone.com/qtSrfl)。那么问题是什么? –