代码之家  ›  专栏  ›  技术社区  ›  MeetJoeBlack

来自url的java剪切文件扩展名

  •  3
  • MeetJoeBlack  · 技术社区  · 10 年前

    我需要像这样从URL中删除文件扩展名:

    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
    

    所以我想得到“svg”和“eot”。 我是这样做的(注意,我希望避免所有规范字符,而不仅仅是“#”):

     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
            }
    

    但也许我能做得更快?

    1 回复  |  直到 10 年前
        1
  •  1
  •   Avinash Raj    10 年前

    你可以试试这个,

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

    DEMO