Is there a way to get all possible match using regex in java :
Java Tester Program :
public class NewClass {
public static void main(String[] args) throws IOException {
String targetFileStr = IOUtils.toString(new FileInputStream(new File("src/SampleHTML.html")), "UTF-8");
Matcher matcher = Pattern.compile("<body>(.|[\\r\\n])*?<link").matcher(targetFileStr);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}
Sample HTML File like :
<!DOCTYPE html>
<html>
<body>
<script src="1"></script>
<link href="1" />
<link href="2" />
<link href="3" />
<div>TODO write content</div>
</body>
</html>
Non-Greedy Regex Current Output : Current Output of program is give below in case of non-greedy regex - "<body>(.|[\\r\\n])*<link"
<body>
<script src="1"></script>
<link
Greedy Regex Current Output : Current Output of program is give below in case of greedy regex - "<body>(.|[\\r\\n])*?<link"
<body>
<script src="1"></script>
<link href="1" />
<link href="2" />
<link
Expected Output : But i need to get all possible match from body to link
1: <body>
<script src="1"></script>
<link
2: <body>
<script src="1"></script>
<link href="1" />
<link
3: <body>
<script src="1"></script>
<link href="1" />
<link href="2" />
<link
Why this Question : I am creating tool that will find and highlight all external style sheet in body .