Get HTML Tags from HTML String in Java | Code Factory


Donate : Link

Medium Blog : Link

Applications : 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

2 thoughts on “Get HTML Tags from HTML String in Java | Code Factory”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s