1

I am currently trying to figure out the regex to grab last part of the path location. I thought of two ways to approach it, either extracting the last part, or use a if statement to figure where there are 2 periods or 3 periods as a search field. I would also to have the last part printed out if possible, I am not familiar with regex.

Examples of the path location are like this /Users/jeremy/desktop/dns/example1.com.csv and /Users/jeremy/desktop/dns/example2.extra.com.csv

Outcomes with the regex is: example1.com.csv or example2.extra.com.csv

Any help is appreciated

Jeremy Lin
  • 248
  • 1
  • 12
  • 1
    What about `[^/]+?(?=[?#]|$)` (using `re.search`) – ctwheels Nov 26 '19 at 21:29
  • 1
    do you have to use regex? if not check out [this answer](https://stackoverflow.com/questions/8384737/extract-file-name-from-path-no-matter-what-the-os-path-format) about using python's `ntpath` module – swaggy p Nov 26 '19 at 21:32
  • 1
    If regex is not absolutely required, I would use [`os.path.basename`](https://docs.python.org/3/library/os.path.html#os.path.basename) or [`PurePath.name`](https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.name). – Alex Nov 26 '19 at 21:32
  • 1
    @Alex ```os.path.basename``` did the trick, thanks for the help. Greatly appreciate it. – Jeremy Lin Nov 26 '19 at 21:41

0 Answers0