我需要改变这样的事情 - > qazxsw POI抢链接,并改变它在我犯了一个方法,并更换它放回这样的字符串
- > Hello, go here http://www.google.com for your ...
这里是我到目前为止有:
Hello, go here http://www.yahoo.com for your...
所有我需要做的就是在字符串URL更改为不同的东西。的URL字符串不会永远是if(Text.toLowerCase().contains("http://"))
{
// Do stuff
}
else if(Text.toLowerCase().contains("https://"))
{
// Do stuff
}
,所以我不能只是说http://www.google.com
使用表达式:
replace("http://www.google.com","")
注:更改代码,以满足下面的评论额外要求。
你可以抓住从用下面的代码串的链接。我假定该字符串将只包含com域
String oldUrl = text.replaceAll(".*(https?://)www((\\.\\w+)+).*", "www$2");
text = text.replaceAll("(https?://)www(\\.\\w+)+", "$1" + traslateUrl(oldUrl));
你有没有尝试过这样的:
String input = "Hello, go here http://www.google.com";
Pattern pattern = Pattern.compile("http[s]{0,1}://www.[a-z-]*.com");
Matcher m = pattern.matcher(input);
while (m.find()) {
String str = m.group();
}
这将找到任何字以http截至第一空白开始,以任何你想要更换
如果你想保持的链接,那么你可以这样做:
s= s.replaceFirst("http:.+[ ]", new link);
你可以试试这个
String oldURL;
if (s.contains("http")) {
String[] words = s.split(" ");
for (String word: words) {
if (word.contains("http")) {
oldURL = word;
break;
}
}
//then replace the url or whatever
}