HTML regex Java

HTML

HTML stands for HyperText Markup Language and is used to display information in the browser. HTML regular expressions can be used to find tags in the text, extract them or remove them. Generally, it’s not a good idea to parse HTML with regex, but a limited known set of HTML can be sometimes parsed.

Match all HTML tags

Below is a simple regex to validate the string against HTML tag pattern. This can be later used to remove all tags and leave text only.

Pattern.compile("<(?:\"[^\"]*\"['\"]*|'[^']*'['\"]*|[^'\">])+>")

Test it!
/<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>/

True

False

Enter a text in the input above to see the result

Example code in Java:

import java.util.regex.Pattern;

public class Main {

    public static void main(String []args) {
        // Remove all HTML tags from a string
        String result = Pattern.compile("<(?:\"[^\"]*\"['\"]*|'[^']*'['\"]*|[^'\">])+>")
               .matcher("<html><body>Hello, <b>world</b>!<br /></body></html>")
               .replaceAll(""); 
        System.out.println(result); // prints Hello, world!
    }
}

Extract text between certain tags

One of the most common operations with HTML and regex is the extraction of the text between certain tags (a.k.a. scraping). For this operation, the following regular expression can be used.

Pattern pattern1 = Pattern.compile("<div>(.*?)<\\/div>"); // Tag only
Pattern pattern2 = Pattern.compile("(?:<div.*?class=\"some-class\".*?>)(.*?)(?:<\\/div>)"); // Tag and class

Test it!
/<div>(.*?)<\/div>/g

True

False

Enter a text in the input above to see the result

Example code in Java:

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Main {

    public static void main(String []args) {
        // Extract text between specific HTML tag
        Matcher matcher = Pattern.compile("(?:<div.*?class=\"some-class\".*?>)(.*?)(?:<\\/div>)")
                          .matcher("<html><body>Probably.<div class=\"some-class\">Hello, world!</div><br />Today</body></html>");
        if (matcher.find()) {
            System.out.println(matcher.group(1)); // prints [Hello, world!]
        }
    }
}
Test it!

True

False

Enter a text in the input above to see the result

Notes on HTML regex

You should never use regular expressions to fully parse HTML documents as regular expressions are not intended for such tasks. Instead, you can use HTML or XML document parsers that can do validation alongside parsing.