Regex strip filename from url

I need to strip links to files fileName like this:

 url('../webfonts/2EC49F_0_0.eot')

      

So I need to cut the path '2EC49F'

, I create this template: ([\/]].*?[_])

But it doesn't work. I am using Java Pattern / Matcher.

+3


source to share


4 answers


I think you can use this

/.*?([^\/]*$)/

      

or



.*(?<=\/)([^_.]*).*$

      

That you can use wildcard \1

.

+1


source


You can use a search based regex to match:

(?<=/)[^/_]+(?=_)

      



Demo version of RegEx

+2


source


You can use the following pattern to match:

([^_\/]*)_

      

and replace it with an empty string.

+1


source


As pointed out in the comments, don't use regex. Instead of this:

String url(String input) {
    int slashLoc = input.lastIndexOf('/');
    int underLoc = input.indexOf('_', slashLoc);
    if (slashLoc != -1 && underLoc && slashLoc+1 < underLoc) { // sanity check
        return input.substring(shashLoc, underLoc);
    } else return "";
}

      

0


source







All Articles