Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex removing filename from path

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

like image 952
RpTheHotrod Avatar asked Sep 11 '14 20:09

RpTheHotrod


2 Answers

You can use a simple regex like this:

(.*\/).*

Working demo

enter image description here

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.

like image 200
Federico Piazza Avatar answered Oct 22 '22 20:10

Federico Piazza


You can use this.

[^\/]+$

'$' matches the position at the end of the string while '[^/]+' matches any string that does not have any '/'.

like image 45
John Aaron Alcoseba Avatar answered Oct 22 '22 22:10

John Aaron Alcoseba