Home > Back-end >  Regex pattern to extract file name from path?
Regex pattern to extract file name from path?

Time:12-23

I'm trying to create a regex pattern to part numbers from file paths like this

\\D6-DC\Users$\name\Profile\Desktop\New folder\XXX-XX-XX-XXX OP1 RAN.NC

Where XXX-XX-XX-XXX is the string i want to extract, the number after OP will sometimes change, and RAN may not always be present. File locations also change.

any help would be greatly appreciated

I'm not very good with regex and have spent almost 2 hours trying to figure this out yesterday. The closest I got was (?:.(?!\\)) $ which is capturing the end of the path okay, but I can't for the life of me figure out how to narrow this down to only outputting the specific string.

CodePudding user response:

If your goal is to extract "XXX-XX-XX-XXX" which has this specific structure. First, you can split your string. Then, in the last part, you can extract the first 13 characters. Here is a snippet can do this.

a = r"""\\D6-DC\Users\name\Profile\Desktop\New folder\XXX-XX-XX-XXX OP1 RAN.NC"""
a.split('\\')[-1][0:13]

CodePudding user response:

You need a regexp to capture the string between the last folder char "\" and the first space after this position.

  1. match all the chars until the last folder char (you have to double \ to escape it): .*\\
  2. Then capture everything that is not a space: ([^\ ] )

Whole answer:

.*\\([^\ ] )

To see it in action: https://regex101.com/r/VBJmaP/1

  • Related