-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
但我必须使用正则表达式 –