Get HTML Tags from HTML String in Java | Code Factory

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")));
}
}
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
!DOCTYPE
html
head
title
body
h1
p

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Technology Keeps on Evolving. Are You Evolving Too?

person working at a computer

Web Crawler — Getting Medium Post

Reducing time to release: minimizing the manual Test Cycle

Technology Valuation

Upload your Rails App to Heroku

A Select List of Kubernetes Tools

Introducing WhaleWatch

Remove boilerplate with RecyclerView.Adapter

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

Java #4 | OOP Object Oriented Programming (1)

Programming in Java - Annotations

Object Oriented Programming in Java

Understanding Abstract Window Toolkit in Java