我需要像这样从 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
}
但也许我可以做得更快?
最佳答案
你可以试试这个,
Matcher m = Pattern.compile("[^/.]*\\.(\\w+)[^/]*$").matcher(s);
if(m.find())
{
System.out.println(m.group(1));
}
关于java从url中剪切文件扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31695454/