Python Regular Expression For Windows File Path
Solution 1:
Here's the expression I got, based on yours, that allow me to get the path on windows : [a-zA-Z]:\\((?:[a-zA-Z0-9() ]*\\)*).*
. An example of it being used is available here : https://regex101.com/r/SXUlVX/1
First, I changed the capture group from ([a-zA-Z0-9() ]*\\)*
to ((?:[a-zA-Z0-9() ]*\\)*)
.
Your original expression captures each XXX\
one after another (eg : Users\
the Users\
).
Mine matches (?:[a-zA-Z0-9() ]*\\)*
. This allows me to capture the concatenation of XXX\YYYY\ZZZ\
before capturing. As such, it allows me to get the full path.
The second change I made is related to the filename : I'll just match any group of character that does not contain \
(the capture group being greedy). This allows me to take care of strange file names.
Another regex that would work would be : [a-zA-Z]:\\((?:.*?\\)*).*
as shown in this example : https://regex101.com/r/SXUlVX/2
This time, I used .*?\\
to match the XXX\
parts of the path.
.*?
will match in a non-greedy way : thus, .*?\\
will match the bare minimum of text followed by a back-slash.
Do not hesitate if you have any question regarding the expressions. I'd also encourage you to try to see how well your expression works using : https://regex101.com . This also has a list of the different tokens you can use in your regex.
Edit : As my previous answer did not work (though I'll need to spend some times to find out exactly why), I looked for another way to do what you want. And I managed to do so using string splitting and joining.
The command is "\\".join(TARGETSTRING.split("\\")[1:-1])
.
How does this work : Is plit the original string into a list of substrings, based. I then remove the first and last part ([1:-1]
from 2nd element to the one before the last) and transform the resulting list back into a string.
This works, whether the value given is a path or the full address of a file.
Program Files (x86)\\Adobe\\Acrobat Distiller\\acrbd.exe fred
is a file path
Program Files (x86)\\Adobe\\Acrobat Distiller\\acrbd.exe fred\
is a directory path
Post a Comment for "Python Regular Expression For Windows File Path"