uNetti uNetti - 1 year ago 90
C++ Question

RegEx for getting path and filename from code requiring path

I been searching for the RegEX to extract files with path from code, so that I could check if they exists but ended up not getting exactly what I wanted. This I guess is most likely due to lack of RegEX knowledge.

I got a bit frustrated about every single RegEx example I ran, I always ended up getting filenames of files without Paths, my requirements are fairly simple in sed kinda way.


I the path needs between 1-6 deep, it needs to be filename with certain extension, the path are unix based but not starting always starting with forward slash. And the filename cannot start with a dot.

Answer Source

I tried this in powershell and it works, I'm sure you can use something like this in your app.

$Matches = @()
$String = '/this/is/a/long/path/and/Filename.MyExtension'
if ($String -match '^(/?[^/]+){1,6}/([^/.][^/]*[.]MyExtension)$') {
    } # end if



This works by looking for between 1 and 6 groups of strings each string must start with a / and the string continues through all non slash characters or the character group must not start with a /

The last group of characters must not start with a / or . followed by all the non slash characters upto the required dot extension [in this case "MyExtension']

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download