English 中文(简体)
C. 名录和档案登记备案
原标题:Regex for directory and file listing in C
  • 时间:2012-05-05 18:15:28
  •  标签:
  • c
  • regex
  • linux

I m trying to write an expression that would filter out several types of directories and files when listing the contents of a directory. Namely, I want to avoid listing the current directory (.), upper directory (..), hidden files and other more specific directories.

这是我现在的事情:

<代码>[+]/[+]/>>

However, it doesn t match ., .., recycle_bin nor cgi-bin. If I remove all the | operands and leave the expression to only [\.+], it works (matches ., .., etc). Which is strange, because I m pretty sure | = OR. Do I miss something?

附录 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
");
                exit(1);
            }

            reti = regexec(&regex, entry->d_name, 0, NULL, 0);
            if( !reti ){

                printf("directoy %s found -> %s", path, entry->d_name);
                printf("
");

            }
            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("
");

            }
            else{
                regerror(reti, &regex, msgbuf, sizeof(msgbuf));
                fprintf(stderr, "Regex match failed: %s
", msgbuf);
            }
最佳回答

http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html#tag_09_04” rel=“nofollow”>“extised REs”。 table (“obsolete”)s,/code>是普通性质。

regcomp(..., REG_EXTENDED);

另见rel=“nofollow”>regcomp(>

问题回答

The pmg s comment and Trial this regex:

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

这是C regex图书馆的顶点。 其宗旨是让你制定方案,接受reg为投入。 这个问题如果得不到解决,就会更好解决:

#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:

你们不理解你目前的做法意味着什么,你可能想像:

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:




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...