Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I get the last name in a path next to the right of a \ character using RE on Python?

I have been trying to make a good regular expression that catches the last name (of a file or folder) for a given path, meaning that for instance, the following path:

C:\Users\ResetStoreX\Pictures

Can be reduced to:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Pictures

What I have achieved so far is the following RE: (?:[^\\]\\)[^\\]*$

Which reduces the previous path to:

X\Pictures

Any ideas?

>Solution :

You almost had it. I removed the \ inside of the parentheses, if you want all backslashes to be captured.

(?:[^\\])[^\\]*$
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading