这种问题是针对正则表达式进行的:
Pattern findUrl = Pattern.compile("\bhttp.*?\.pdf\b");Matcher matcher = findUrl.matcher("This is a URL http://www.google.com/MyDoc.pdf which should be used");while (matcher.find()) { System.out.println(matcher.group());}正则表达式说明:
b
在“ http”之前有一个单词边界(即xhttp不匹配)http
字符串“ http”(请注意,这也匹配“ https”和“ httpsomething”).*?
任何字符(.
)任意次(*
),但尝试使用最少数量的字符(?
).pdf
文字字符串“ .pdf”b
在“ .pdf”之后有一个单词边界(即.pdfoo不匹配)
如果您只想匹配http和https,请尝试使用它而不是
http字符串:
https?:
-匹配字符串http,然后是可选的“ s”(?
在s之后表示),然后是冒号。



