3

I need to cut file extension from urls like this:

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'. Im doing it like this(note that I want to avoid all spec chars 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?

1
  • 2
    Use URI class to parse the URL into its parts, take the path and split by /, then look for the extension. Commented Jul 29, 2015 at 8:46

1 Answer 1

1

You may try this,

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

DEMO

Sign up to request clarification or add additional context in comments.

1 Comment

This will give wrong result for URL like example.com/#/path/do.nothing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.