开始 (^) 和结束 ($) 锚点不起作用
Start (^) and end ($) anchors not working
基本上我在我的 C 程序中使用以下模式(参见 ):
^[0-9]( [0-9])*$
使用以下代码:
char *pattern = "^[0-9]( [0-9])*$";
regex_t regexCompiled;
int rc = regcomp(®exCompiled, pattern, REG_EXTENDED);
if (rc != 0) {
char msgbuf[100];
regerror(rc, ®exCompiled, msgbuf, sizeof (msgbuf));
fprintf(stderr, "Regex match failed: %s\n", msgbuf);
exit(EXIT_FAILURE);
}
if (regexec(®exCompiled, "0 1", 0, NULL, REG_EXTENDED) == 0) {
printf("Valid\n");
} else {
printf("Invalid\n");
}
我在其中针对字符串“0 1”执行,该字符串对模式有效但不起作用。 '^' 和 '$' 不起作用。这是为什么?我怎样才能让它发挥作用?
您正在将 REG_EXTENDED
传递给 regexec()
,这不是该调用的有效标志。
eflags may be the bitwise-or of one or both of REG_NOTBOL
and REG_NOTEOL
which cause changes in matching behavior described below.
可能 REG_EXTENDED
的实际值与其中之一匹配。
更改代码以将 0
作为最后一个参数传递给 regexec()
使其匹配。
基本上我在我的 C 程序中使用以下模式(参见
^[0-9]( [0-9])*$
使用以下代码:
char *pattern = "^[0-9]( [0-9])*$";
regex_t regexCompiled;
int rc = regcomp(®exCompiled, pattern, REG_EXTENDED);
if (rc != 0) {
char msgbuf[100];
regerror(rc, ®exCompiled, msgbuf, sizeof (msgbuf));
fprintf(stderr, "Regex match failed: %s\n", msgbuf);
exit(EXIT_FAILURE);
}
if (regexec(®exCompiled, "0 1", 0, NULL, REG_EXTENDED) == 0) {
printf("Valid\n");
} else {
printf("Invalid\n");
}
我在其中针对字符串“0 1”执行,该字符串对模式有效但不起作用。 '^' 和 '$' 不起作用。这是为什么?我怎样才能让它发挥作用?
您正在将 REG_EXTENDED
传递给 regexec()
,这不是该调用的有效标志。
eflags may be the bitwise-or of one or both of
REG_NOTBOL
andREG_NOTEOL
which cause changes in matching behavior described below.
可能 REG_EXTENDED
的实际值与其中之一匹配。
更改代码以将 0
作为最后一个参数传递给 regexec()
使其匹配。