删除字符串中的一系列字符

时间:2012-03-30 12:48:26

标签: java regex

我需要在"?"之后删除字符串中的所有字符。这样标记:

http://xyz.com//static/css/style.css?v=e9b34

但是我不能用#34;"吐出来,我需要确保模式是.xyz?any_char_or_symbols

换句话说extension-?-any_chars_or_symbols

任何人都可以提示吗?

3 个答案:

答案 0 :(得分:1)

我在这里遗漏了一些东西,或者这样做了:

String s = "http://xyz.com//static/css/style.css?v=e9b34";
int index = s.indexOf('?');
if (index<0)
    return null;
String returned = s.substring(0,index);
if (returned.endsWidth(".xyz");
    return returned;
else
    return null;

答案 1 :(得分:1)

return s.replaceFirst("(\\.\\w+\\?).*$", "$1");

答案 2 :(得分:0)

首先删除字符后?然后检查是否有扩展。

String url="http://xyz.com//static/css/style.css?v=e9b34"   ;

url=url.substring(0, url.indexOf("?"));

System.out.print(""+url.matches("^[\\w\\d\\:\\/\\.]+\\.\\w{3}(\\?[\\w\\W]*)?$"));

url.matches将检查扩展名。我已将它定义为3个单词w {3},但您可以将其增加为w {3,5}。现在它将接受3到5个字的扩展。如果你想要特定的格式,那么请使用这个模板。

url.matches("^[\\w\\d\\:\\/\\.]+\\.(?i)(css|txt|html)?$")

希望它会有所帮助。