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

Time:12-26

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:

Pictures

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

Which reduces the previous path to:

X\Pictures

Any ideas?

CodePudding user response:

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

(?:[^\\])[^\\]*$
  • Related