在 Java 中,使用 正则表达式(regex) 抓取网址,通常步骤如下:
Pattern 和 Matcherhttps?://[\\w./?=&%-]+
说明:
https?://:匹配 http 或 https[\\w./?=&%-]+:匹配网址常见字符import java.util.regex.*;
import java.util.*;
public class UrlExtractor {
public static void main(String[] args) {
String text = "访问 https://www.example.com/path?name=test 和 http://test.com 查看详情";
String regex = "https?://[\\w./?=&%-]+";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
List<String> urls = new ArrayList<>();
while (matcher.find()) {
urls.add(matcher.group());
}
urls.forEach(System.out::println);
}
}
输出:
https://www.example.com/path?name=test
http://test.com
如果你希望更规范一点:
(https?://)([\\w-]+\\.)+[\\w-]+(/[\\w./?=%&:-]*)?
如果你有:
可以告诉我,我可以帮你写更精准的正则和代码。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。