gpt4 book ai didi

java - 使用正则表达式匹配 url

转载 作者:行者123 更新时间:2023-11-30 04:57:45 25 4
gpt4 key购买 nike

我采用这个 (?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*\. (?:jpg|gif|png))(?:\?([^#]*))?(?:#(.*))? 来自此 answer 的正则表达式。如果我在下面的程序中使用它来匹配 url 意味着我收到编译器错误。

这是我的代码:

public static void main(String[] args) {
String url="http://justfuckinggoogleit.com/bart.gif";
matchesImageUrl(url);

}
public static void matchesImageUrl(String url){
Pattern imagePattern=Pattern.compile("(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*\. (?:jpg|gif|png))(?:\?([^#]*))?(?:#(.*))?");

if(imagePattern.matcher(url).matches()){

System.out.println("image matches with the pattern" + url);


}
else{

System.out.println("image does not matches with the pattern");

}


}

最佳答案

你需要逃脱两次。

因此将 \ 替换为 \\

See it work

关于java - 使用正则表达式匹配 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7995375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com