我有一个测试网址:
http://www.abc.com/ABC/ABC-Boots-in-Leather/Prod/product.aspx?iid=34487
我必须将其转换为:
http://www.abc.com/product.aspx?iid=34487
我正在使用正则表达式:
String u = url.replaceAll("/.*?/","");
但它不会删除文本,只是删除斜杠。我该如何纠正呢?
答案 0 :(得分:3)
int x = url.indexOf('/');
int y = url.lastIndexOf('/')+1;
String u = url.substring(0, x) + url.substring(y);
答案 1 :(得分:2)
String s = "http:www.abc.com/ABC/ABC-Boots-in-Leather/Prod/product.aspx?iid=34487";
String s1 = s.replaceAll("(/(.)*/)","/");
输出:http:www.abc.com/product.aspx?iid=34487
答案 2 :(得分:0)
已更新以更改问题
可能不是最简单的解决方案,但有效
String url = "http://www.abc.com/ABC/ABC-Boots-in-Leather/Prod/product.aspx?iid=34487";
String u = "http:/" + url.replaceAll("(http://)|/.*/", "/");
结果:
http://www.abc.com/product.aspx?iid=34487