import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class UrlUtils {
public static String getUrl(String input) {
String regex = "(https://.*?)[\\s\u4e00-\u9fa5]";
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
return matcher.group(1);
}
return "";
}
}
测试结果如下:
import java.net.MalformedURLException;
public class test {
public static void main(String[] args) throws MalformedURLException {
String cr = "小雪将至,一转眼秋天变成了故事,冬天变成了风景,愿你有衣暖身,有人暖心。”%%广东将迎强冷空气 %%小雪 https://v.douyin.com/Rp8SceR/ 复制此链接,打开Dou音搜索,直接观看视频!";
String url = UrlUtils.getUrl(cr);
System.out.println("url为:"+url);
}
}
//打印结果
url为:https://v.douyin.com/Rp8SceR/