如何匹配的URL字符串是這樣的:Java的正則表達式匹配器問題
IMG SRC =「https://stackoverflow.com/a/b/c/d/someimage.jpg」
僅有的則是變量,域名和文件擴展名(JPG)是固定的?
下面的代碼似乎並不工作:
Pattern p = Pattern.compile("<img src=\"http://stachoverflow.com/.*jpg");
// Create a matcher with an input string
Matcher m = p.matcher(url);
while (m.find()) {
String s = m.toString();
}
也許是因爲它實際上是用計算器一'k',而不是'h'? ;)此外,正確的方法將使用HTML解析器:http://stackoverflow.com/search?q=parse+html+with+regex – BalusC 2010-04-04 04:17:08
這是一個問題,正好在正則表達式的範圍內,很多HTML解析不適合正則表達式,但我沒有看到用這種方式提取圖像路徑的問題。 – Michael 2010-04-04 05:09:01
@Michael:''可能會成爲一個問題,具體取決於正則表達式的用途...... – 2010-04-04 08:01:47