Thursday, April 13, 2017

REGEX | How to extract Path from full Path

Case 1

The case if you want to extract path from a full path directory ex:

D://Folder/Folder/filename.extension

Output

D://Folder/Folder/

Here is a basic example of an inverse regex (finds the filename). Just use the substring before the start to get the path.


[\w]*[.][\w]*$
And the path only:

.*[\\]

Case 2

Case 2 is you get a full path like this


$/Server/First Level Folder/Second_Level_Folder/My File.extension


 this is possible (\$/.*?\.\S*) should do the job just fine.
\$/ matches the start of the path
.*? matches everything till the next part of the regex
\.\S* matches the dot and anything but a whitespace (space, tab)
And the ( ) around it make it capture all that is matched.
EDIT:
For further use
Just the path
(\$/.*?/)[^/]*?\.\S*
Just the filename
\$/.*?/([^/]*?\.\S*)

0 komentar:

Post a Comment