Home > front end >  c regular expression to list all files in a directory except hidden files (start with .)
c regular expression to list all files in a directory except hidden files (start with .)

Time:01-13

I have a string array of the list of directory contents.

I want to list all the strings that don't start with "."

So, from the list, the regex should not include ".hid.mp3", ".Hidden.txt".

Can someone suggest the fileRegex for the following code?

string fileList[] = {"test.png", "Hellozip.zip", "123.mp3", "hid.mp3", "hid.file.png", "sp ace.png", "comm(a.mp3", ".Hidden.txt"};

for(auto& file : fileList)
{
    if (std::regex_match(file, std::regex(fileRegex, std::regex_constants::icase)) == true)
    {
        cout << file << " - Match\n";
    }
    else
    {
        cout << file << " - No Match\n";
    }
}

Expected output should be:

test.png - Match
Hellozip.zip - Match
123\.mp3 - Match
.hid.mp3 - No Match
hid.file.png - Match
sp ace.png - Match
comm(a.mp3 - Match
.Hidden.txt - No Match

I tried this, but it did not work:

"\[\\w-\]*\\.{0,1}\[\\w- ()\]*\\.(png|jpg|zip|mp3|txt)"

Edited: I can handle ".", ".." as special case, so removed from the question.

CodePudding user response:

how about

 ^[^.].*$

seems to work

  • Related