-1
我有一個大字符串,我想從該字符串中取得鏈接。我可以打印鏈接。從replace取代變量全部
Pattern pattern = Pattern.compile(".*(?<=overlay-link\" href=\").*?(?=\">).*");
與該代碼。輸出示例:
<a title="TITLE" class="overlay-link" href="LINK HERE"></a>
當我嘗試string.replaceAll,正則表達式刪除鏈接並打印另一個變量。
EX: <a title="TITLE" class="overlay-link" href=""></a>
我是新的正則表達式。你可以幫我嗎?
這裏是全碼:
String content;
Pattern pattern = Pattern.compile(".*(?<=overlay-link\" href=\").*?(?=\">).*");
try {
Scanner scanner = new Scanner(new File("sourceCode.txt"));
while (scanner.hasNext()) {
content = scanner.nextLine();
if (pattern.matcher(content).matches()) {
System.out.println(content.replaceAll("(?<=overlay-link\" href=\").*?(?=\">)", ""));
}
}
} catch (IOException ex) {
Logger.getLogger(SourceCodeExample.class.getName()).log(Level.SEVERE, null, ex);
}
不要使用正則表達式解析XML或HTML。請參閱http://stackoverflow.com/questions/701166/can-you-provide-some-examples-of-why-it-is-hard-to-parse-xml-and-html-with-a-reg – VGR
但我必須使用正則表達式 –