我正在尝试编写一个表达式,在列出目录内容时过滤掉几种类型的目录和文件。也就是说,我想避免列出当前目录 (.)、上层目录 (..)、隐藏文件和其他更具体的目录。
这是我现在拥有的:
[\\.+]|cgi-bin|recycle_bin
但是,它不匹配.
、..
、recycle_bin 或cgi-bin
。如果我删除所有 |
操作数并将表达式仅保留为 [\\.+]
,它会起作用(匹配 .
、 ..
等)。这很奇怪,因为我很确定 |
= OR。我错过了什么吗?
更新 1:这是我使用的代码:
regex_t regex;
int reti;
char msgbuf[100];
/* Compile regular expression */
reti = regcomp(®ex, "[\\.+]|cgi-bin|recycle_bin", 0);
if( reti )
{
fprintf(stderr, "Could not compile regex\n");
exit(1);
}
reti = regexec(®ex, entry->d_name, 0, NULL, 0);
if( !reti ){
printf("directoy %s found -> %s", path, entry->d_name);
printf("\n");
}
else if( reti == REG_NOMATCH ){
//if the directory is not filtered out, we add it to the watch list
printf("good dir %s", entry->d_name);
printf("\n");
}
else{
regerror(reti, ®ex, msgbuf, sizeof(msgbuf));
fprintf(stderr, "Regex match failed: %s\n", msgbuf);
}
最佳答案
使用"extended REs" .用regular ("obsolete") ones , |
是一个普通字符。
regcomp(..., REG_EXTENDED);
关于c - C中目录和文件列表的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10464646/