How could I design a RegEx script which would remove a filename from a path? The trick is, the paths have all sorts of wrenches to throw into the works.
Paths can consist of:
1: "Folder1/Folder2/Folder3.1234/folder4.5678/ApplesandOranges.txt","MoreInfo","EvenMoreInfo"
2: "Folder1/Folder2/Folder3.1234/folder4.91011/","MoreInfo","EvenMoreInfo"
or even
3: "Folder1/Folder2/Folder3.1234/folder4.5678/ApplesandOranges.zip?CatsAndDogs.txt","MoreInfo","EvenMoreInfo"
In cases 1 and 3, I'd like to end up with:
Folder1/Folder2/Folder3.1234/folder4.5678/
though, it would be acceptable for the second to return as
Folder1/Folder2/Folder3.1234/folder4.5678/ApplesandOranges.zip
though not preferred.
In case 2, it'd just skip that line entirely as there is no filename.
Any suggestions?
Using standard RegEx via a text editor. No java use and such.
Note: The path is just an example. There could be 50 folders. It's not a mere 4 folders all of the time
You can use a simple regex like this:
(.*\/).*
Working demo
As you can see, the idea is to capture all the content to the last slash by using (.*\/)
and discard the rest .*
. Check the substitution section above.
You can use this.
[^\/]+$
'$' matches the position at the end of the string while '[^/]+' matches any string that does not have any '/'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With