c中的正则表达式工作不正常
我想匹配c中的正则表达式工作不正常,c,regex,C,Regex,我想匹配“16KQGN579677 PT” 这是我的尝试: ^([A-Z0-9]{2}+)([A-Z]{4}+)([0-9]{1,6}+)([ ]{0,5}+)([A-Z]{1,4}+).*" 这是代码: int main(int argc, char *argv[]){ regex_t str; int reti; char msgbuf[100]; /* need to match this circuit(16KQGN579677 PT) */
“16KQGN579677 PT”
这是我的尝试:
^([A-Z0-9]{2}+)([A-Z]{4}+)([0-9]{1,6}+)([ ]{0,5}+)([A-Z]{1,4}+).*"
这是代码:
int main(int argc, char *argv[]){
regex_t str;
int reti;
char msgbuf[100];
/* need to match this circuit(16KQGN579677 PT) */
reti=regcomp(&str,"^([A-Z0-9]{2}+)([A-Z]{4}+)([0-9]{1,6}+)([ ]{0,5}+)([A-Z]{1,4}+).*", 0);
if( reti )
{
fprintf(stderr, "Could not compile regex\n");
exit(1);
}
/* need to match this circuit(16KQGN579677 PT) */
reti = regexec(&str, "16KQGN579677 PT", 0, NULL, 0);
if( !reti ){
puts("Match");
}
else if( reti == REG_NOMATCH ){
puts("No match");
}
else {
regerror(reti, &str, msgbuf, sizeof(msgbuf));
fprintf(stderr, "Regex match failed: %s\n", msgbuf);
exit(1);
}
regfree(®ex);
return 0;
}
代码中的正则表达式有什么问题?请尝试以下方法:
([0-9]{2}\w+\s+[A-Z]{1,4})
Match the regular expression below and capture its match into backreference number 1 «([0-9]{2}\w+\s+[\w]{2})»
Match a single character in the range between “0” and “9” «[0-9]{2}»
Exactly 2 times «{2}»
Match a single character that is a “word character” (letters, digits, and underscores) «\w+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match a single character that is a “word character” (letters, digits, and underscores) «[\w]{2}»
Exactly 2 times «{2}»
你试过:
^([A-Z0-9]{2}+)([A-Z]{4}+)([0-9]{1,6}+)([{0,5}+)([A-Z]{1,4}+)$
?我认为{2}+
是无效的。所有格修饰符只有在{min,max}
或{min,}
之后才有意义。谢谢,它在标志中使用$and REG_扩展而不是“0”@Barmar:如果它像PCRE,{2}+
只是冗余的(并且没有用处),但不是无效的。为什么post“16KQGN579677 PT”
以空格开头/结尾,但code“16KQGN579677 PT”
是否不适用?