简体   繁体   English

用于url css文件名和扩展名的Java正则表达式

[英]java regex for url css filename and extension

I want to get the whatevername.css from url's but I'm doing something wrong, the urls might change and the names also so it could be 我想从网址中获取whatname.css,但是我做错了,网址可能会更改,名称也可能会更改

www.asdasad.as/asdas/asdas/mystyles.css www.asdasad.as/asdas/asdas/mystyles.css

asda.com/styles.css etc etc asda.com/styles.css等

I've tried with this (but it doesn't work "cannot return value from method whose result is void"): 我已经试过了(但是“无法从结果为空的方法返回值”不起作用):

String fileName = "www.whateverpage.es/style.css";
int idx = fileName.replaceAll("\\", "/").lastIndexOf("/");
return idx >= 0 ? fileName.substring(idx + 1) : fileName;

replaceAll uses regex as parameter and to represent \\ literal in regex engine you need to pass \\\\ literal so you would need to write it as "\\\\\\\\" String replaceAll使用正则表达式作为参数,并在正则表达式引擎中表示\\文字,您需要传递\\\\文字,因此您需要将其写为"\\\\\\\\"字符串

int idx = fileName.replaceAll("\\\\", "/").lastIndexOf("/");

To get rid of this madness try maybe replace('\\\\','/') instead replaceAll . 为了摆脱这种疯狂,请尝试replace('\\\\','/')代替replaceAll This method wont use regex, but only character switch, so your code can look like 此方法将不使用正则表达式,而仅使用字符切换,因此您的代码看起来像

int idx = fileName.replace('\\', '/').lastIndexOf("/");

Edit after your update 更新后编辑

cannot return value from method whose result is void 无法从结果为空的方法返回值

this error is caused by fact that return type of your method is void instead of String . 此错误是由以下事实引起的:方法的返回类型为void而不是String Change your method declaration from void yourMethodsName() to String yourMethodsName() . 将方法声明从void yourMethodsName()更改为String yourMethodsName()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM