에서 추출 링크는 다음 코드를자바 정규식 - HTML 앵커
private String anchorRegex = "\\<\\s*?a\\s+.*?href\\s*?=\\s*?([^\\s]*?).*?\\>";
private Pattern anchorPattern = Pattern.compile(anchorRegex, Pattern.CASE_INSENSITIVE);
String content = getContentAsString();
Matcher matcher = anchorPattern.matcher(content);
while(matcher.find()) {
System.out.println(matcher.group(1));
}
호출 getContentAsString()
에 반환하는 웹 페이지에서 HTML 콘텐츠를 가지고있다. 내가 가지고있는 문제는 내 System.out에 인쇄 된 유일한 것이 공간이라는 것입니다. 누구든지 내 정규식에 어떤 문제가 있는지 확인할 수 있습니까?
가끔 Regex가 나를 미치게합니다.
그것은 나쁜 생각,이 작업을 수행합니다 : 는 http://stackoverflow.com/questions/590747/using-regular-expressions-to-parse-html-why-not –
그는 아니에요 일반적으로 일치하는 HTML; regex는 제한된 유스 케이스에서 HTML에 적합합니다. – beerbajay