I needed a way to get the filename from arbitrary pathnames in python, but sometimes it is faster to code a solution in the language you are most comfortable with, then figure out how to rewrite it in the language you need. I often end up finding issues with my approach just by writing it, but harder to do that if I’m getting stuck on syntax.
Example Inputs:
- example.txt
- path/blah whatever/file.txt
- path\path blah \file whatever.txt
- path blah this}file.txt
- path blah this}file example.txt
Desired Outputs:
- example.txt
- file.txt
- file whatever.txt
- file.txt
- file example.txt
I came up with this solution using BigFix relevance quickly, because I know relevance rather well, but also, it is very good at string parsing compared to most other languages:
Q: ( following text of (lasts "/" of it; lasts "\" of it; lasts "}" of it) | it ) of ("example.txt";"path/blah whatever/file.txt";"path\path blah \file whatever.txt";"path blah this}file.txt";"path blah this}file example.txt")
A: example.txt
A: file.txt
A: file whatever.txt
A: file.txt
A: file example.txt
T: 0.887 ms
I: plural string
This solves my problem, but it is quite useless in python.
I then consulted this answer to try to figure out the best way to do this in python: https://stackoverflow.com/questions/12572362/how-to-get-a-string-after-a-specific-substring
I then looked over all the answers and then promptly gave up and decided I’d write a Regular Expression instead, which would work in BOTH python and bigfix relevance.
I often use these sites to help write regex:
Then I came up with this solution in RegEx:
Q: parenthesized parts 3 of first matches (regex "(.*(\/|\\|\}))*(.*)") of ( "example.txt" ; "path/blah whatever/file.txt" ; "path\path blah \file whatever.txt" ; "path blah this}file.txt" ; "path blah this}file example.txt" )
A: example.txt
A: file.txt
A: file whatever.txt
A: file.txt
A: file example.txt
T: 0.933 ms
I: plural substring
Now I just need to implement the same in python. (or any other language)
I’m not a huge fan of Regular Expressions in general, but they are a common language for string parsing that works almost anywhere, though not always in a consistent way.
I ended up implementing this in python without using RegEx with a suggestion from @jtavan https://github.com/jgstew/tools/blob/master/Python/get_filename_from_pathname.py
Related:
- https://bigfix.me/relevance/details/3022959
- https://twitter.com/jgstew/status/1272974527908188162?s=20
- https://stackoverflow.com/questions/12572362/how-to-get-a-string-after-a-specific-substring#comment16938235_12572448
- https://github.com/jgstew/tools/blob/master/Python/get_filename_from_pathname.py