Search code examples
c++regexpathfilepathunc

Regex: split to the last occurence of path


I want to split up an UNC-path for hostname, shared folder, path, filename and extension. I almost got it, but the last sequence is somehow wrong because I didn't get the filenaem correctly.

e.g.

//host/shared/path1/path2/path3/filename.pdf

should be split up to:

host
shared
path1/path2/path3
filename
pdf

But at the moment I get something like this:

host
shared
path1/path2/path3/filenam
e
pdf

using this regex:

std::regex rgx("\/\/(\\w+?){1,1}\/(\\w+?)\/([\\w\/]+)([^\\.])\\.(.+)$");

So what is wrong with it and how can I solve it?


Solution

  • You want to remove the group "([^\\.])" as the following "\\." matches the period at the end. You also want another word group to match the file name itself that is followed by the period like so:

    std::regex rgx("\/\/(\\w+?){1,1}\/(\\w+?)\/([\\w\/]+)\/([\\w]+)\\.(.+)$");
    

    https://regex101.com/r/yK4zH1/4