Java file extension for extensions from url

I need to strip the file extension from urls like:

http://static.gazeta.ru/nm2012/fonts/light/pts_regular_caption.svg#PTSans-CaptionBold

http://2.cdn.echo.msk.ru/assets/../fonts/echo_font-v5.eot#iefix

      

So, I want to get 'svg' and 'eot'. I do it like this (note that I want to avoid all BOM characters not only "#"):

 String extension = path.substring(path.lastIndexOf(".") + 1) ;   //cut after dot path
     extension = extension.replaceAll("[^A-Za-z0-9]", ";");   // replace all special character on ";"

        if(extension.indexOf(";") > 0) {
            extension = extension.substring(0, extension.indexOf(";")); // cut extension before first spec. char
        }

      

But maybe I can do it faster?

+3


source to share


1 answer


You can try this,

Matcher m = Pattern.compile("[^/.]*\\.(\\w+)[^/]*$").matcher(s);
if(m.find())
{
System.out.println(m.group(1));
}

      



DEMO

+1


source







All Articles