Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C中目录和文件列表的正则表达式_C_Regex_Linux - Fatal编程技术网

C中目录和文件列表的正则表达式

C中目录和文件列表的正则表达式,c,regex,linux,C,Regex,Linux,我正在尝试编写一个表达式,在列出目录内容时,它将过滤掉几种类型的目录和文件。也就是说,我希望避免列出当前目录(.)、上层目录(..)、隐藏文件和其他更具体的目录 这就是我现在拥有的: [\\.+]| cgi站|回收站 但是,它与、、、、回收站或cgi站不匹配。如果删除所有的操作数,并将表达式仅保留为[\\.+],则它可以工作(匹配,。,等等)。这很奇怪,因为我很确定|=OR。我错过什么了吗 更新1:以下是我使用的代码: regex_t regex;

我正在尝试编写一个表达式,在列出目录内容时,它将过滤掉几种类型的目录和文件。也就是说,我希望避免列出当前目录(.)、上层目录(..)、隐藏文件和其他更具体的目录

这就是我现在拥有的:

[\\.+]| cgi站|回收站

但是,它与
、回收站或
cgi站不匹配。如果删除所有的
操作数,并将表达式仅保留为
[\\.+]
,则它可以工作(匹配
,等等)。这很奇怪,因为我很确定
|
=OR。我错过什么了吗

更新1:以下是我使用的代码:

            regex_t regex;
            int reti;
            char msgbuf[100];

            /* Compile regular expression */
            reti = regcomp(&regex, "[\\.+]|cgi-bin|recycle_bin", 0);


            if( reti )
            { 
                fprintf(stderr, "Could not compile regex\n");
                exit(1);
            }

            reti = regexec(&regex, 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, &regex, msgbuf, sizeof(msgbuf));
                fprintf(stderr, "Regex match failed: %s\n", msgbuf);
            }
使用。在中,
|
是一个普通字符

regcomp(..., REG_EXTENDED);

另请参见.p>Fllow pmg的注释并尝试此正则表达式:

^([.]{0,2}|cgi-bin|recycle_bin)$

[.]{0,2}
它匹配

这不是C正则表达式库的用途。它的目的是让您构建接受regexen作为输入的程序。如果没有正则表达式,这个问题会得到更好的解决:

#define SIZE(x) (sizeof (x)/sizeof(*(x)))
char *unwanted[] = {
     ".",
     "cgi-bin",
     "recycle_bin",
};
int x;
for(x=0; x<SIZE(unwanted); x++)
     if(strstr(entry->d_name, unwanted[x])!=NULL)
           goto BadDir;
//good dir
BadDir:
#定义大小(x)(sizeof(x)/sizeof(*(x)))
字符*不需要的[]={
".",
“cgi bin”,
“回收站”,
};
int x;
对于(x=0;xd_名称,不需要的[x])=空)
后藤巴迪尔;
//好导演
巴迪尔:
忽略当前正则表达式的含义,您可能需要以下内容:

char *begins[] = {".", "private_"};
char *equals[] = {"recycle_bin", "cgi-bin"};
char *contains[] = {"_reject_"};

for(x=0; x<SIZE(begins); x++)
    if(strncmp(entry->d_name, begins[x], strlen(begins[x]))==0)
          goto BadDir;
for(x=0; x<SIZE(equals); x++)
    if(strcmp(entry->d_name, equals[x])==0)
          goto BadDir;
for(x=0; x<SIZE(contains); x++)
    if(strstr(entry->d_name, contains[x])!=NULL)
          goto BadDir;
//good dir...
BadDir:
char*开始[]={.“,”private_uu};
char*等于[]={“回收站”、“cgi站”};
char*包含[]={“\u拒绝”};
对于(x=0;xd_名称,从[x]开始),strlen(从[x]开始)为=0)
后藤巴迪尔;
对于(x=0;xd_名称,等于[x])==0)
后藤巴迪尔;
对于(x=0;xd_名称,包含[x])=空)
后藤巴迪尔;
//好导演。。。
巴迪尔:

如何使用此正则表达式?你能把密码贴出来吗?谢谢。我知道这应该很容易。我甚至读了一些关于REG_扩展选项的文章,但是很匆忙,没有抓住要点。再次感谢!首先,您的代码与我的正则表达式所做的事情不同(Dave;)。仔细看看。那么,如果我想匹配所有以“private”开头的目录呢。最后,我不认为性能是一个问题,两者之间可能存在差异,我没有进行基准测试,但我认为差异不大。啊,我在其他答案中看到了
“^…$”
,并假设。然而,在c中使用正则表达式库并不是正确的做法。Perl?当然。@Valentiradu P.S.现在,它没有,也没有:)我让你找出区别!否则,我不认为我同意你所说的“在c语言中这样做是不对的”。你有什么特别的理由这么认为吗?事实上,是的;仔细看看你的正则表达式,看看原因。还有,这就是为什么它在C中不是正确的。