([A-Z]{1}[a-z]+)+([_]{1}([A-Z]{1}[a-z]+)+)+[.][a-z]+
成功匹配以下文件名:
Butter_Butter.jpg
JavaPiebald_Java_Piebald.jpg
LowWhitePied_Pied.jpg
Piebald_Piebald.jpg
SpinnerBlast_Spider_Pinstripe_Pastel.jpg
Caramel_Caramel.jpg
LightningPied_Pied_Axanthic.jpg
Pastel_Pastel.jpg
Spider_Spider.jpg
Spinner_Spider_Pinstripe.jpg
当我在以下 C 代码中实现正则表达式时,我没有收到任何匹配项:
#define COLLECTION_REGEX "([A-Z]{1}[a-z]+)+([_]{1}([A-Z]{1}[a-z]+)+)+[.][a-z]+"
int is_valid_filename(char *filename)
{
regex_t regex;
int i, match;
char msgbuf[100];
match = 1;
i = regcomp(®ex, COLLECTION_REGEX, 0);
if (i)
{
perror("Could not compile regex");
}
else
{
match = regexec(®ex, filename, 0, NULL, 0);
if (!match)
{
puts("Match");
}
else if (match == REG_NOMATCH)
{
puts("No match");
}
else
{
regerror(match, ®ex, msgbuf, sizeof(msgbuf));
puts(msgbuf);
}
}
regfree(®ex);
return match;
}
后续执行:
./a.out
No match
No match
No match
No match
No match
No match
No match
No match
No match
No match
正则表达式看似正确,我不确定为什么我会得到这些结果。
GDB的输出:
Breakpoint 1, is_valid_filename (filename=0x609050 "Piebald_Piebald.jpg") at crp-web-builder.c:76
76 match = regexec(®ex, filename, 0, NULL, 0);
(gdb) continue
Continuing.
No match
Breakpoint 1, is_valid_filename (filename=0x609460 "LightningPied_Pied_Axanthic.jpg") at crp-web-builder.c:76
76 match = regexec(®ex, filename, 0, NULL, 0);
(gdb) continue
Continuing.
No match
Breakpoint 1, is_valid_filename (filename=0x609870 "SpinnerBlast_Spider_Pinstripe_Pastel.jpg") at crp-web-builder.c:76
76 match = regexec(®ex, filename, 0, NULL, 0);
(gdb) continue
Continuing.
No match
答案 0 :(得分:1)
感谢Jonathan Leffler。这一行:
i = regcomp(®ex, COLLECTION_REGEX, REG_EXTENDED);
应该是:
public class MyApplication extends MultiDexApplication{
}