0

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.

luiscla27
  • 4,956
  • 37
  • 49
Maru
  • 1
  • 1
    Does this answer your question? [Regex for extracting filename from path](https://stackoverflow.com/questions/9363145/regex-for-extracting-filename-from-path) – Piotr Siupa Dec 22 '22 at 18:04
  • Assuming the XXX-XX-XX-XXX pattern is distinctive and X can be any character then the following regex pattern should work: r".{3}-.{2}-.{2}-.{3}" (Python) – rhurwitz Dec 22 '22 at 18:07

3 Answers3

0

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]
0

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

Philippe K
  • 143
  • 5
0

Finally solved it myself; not sure how efficient this is but it works for me, so:

^\\(.+\\)*(.+)\s*(?:OP\d)\s*(?:.*)\.(.+)$
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Maru
  • 1