Get HTML Tags from HTML String in Java | Code Factory

Reference Link : Link

Donate : Link

GetHTMLTags.java

package com.codeFactory;import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class GetHTMLTags {public static void main(String... args) throws IOException {
Pattern pattern = Pattern.compile("<([^\\s>/]+)");
Matcher matcher = pattern.matcher(getHTML());
while(matcher.find()) {
System.out.println(matcher.group(1));
}
}

private static String getHTML() throws IOException {
return new String(Files.readAllBytes(Paths.get("TestHTML.html")));
}
}

TestHTML.html

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
</body>
</html>

Output :

!DOCTYPE
html
head
title
body
h1
p