Open In App

Java Program to Extract an HTML Tag from a String using RegEx

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will find/extract an HTML tag from a string with help of regular expressions. The Regular Expression Regex or Rational Expression is simply a character sequence that specifies a search pattern in a particular text. It can contain a single character or it can have a complex sequence of characters. In Java, there is no class for Regular expressions but it uses the java.util.regex package in order to deal with regular expressions. The following classes are present inside the java.util.regex package:

  1. Pattern Class: The object of the Pattern class is the compiled form of the Regular Expression. Pattern doesn’t have any public constructor so it makes use of the compile method which is a static method to create the pattern object.
  2. Matcher Class: As the name suggests this class object matches the pattern of the input string with the pattern class object. Similar to the pattern class, the Matcher class also does not have any public constructor so we get the Matcher object with the help of the matcher method present in the Pattern class.

We are using the above classes to find and extract the content within the HTML tags. 

Steps to Be Followed

Step 1: 

Import necessary library and classes – java.util.regex.Matcher, java.util.regex.Pattern;

Step 2: 

Create an object of the pattern class and pass the regular expression which will represent the required HTML tag as a parameter to compile the function.

Step 3:

Using the matcher() function match it with the string from which we want to detect the HTML tags.

Step 4: 

Use the find method under the Matcher class to see if any instance of the HTML tag is present inside the provided string or not.

Step 5:

If any instance of the HTML tag is present inside the given string the using group() function under the Matcher class is to retrieve the matched instance of the string.

Code

Java




// importing necessary library and classes
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class Main {
    public static void main(String[] args)
    {
        String str = "Learning from <h1>GeeksforGeeks<h1>";
       
        // pattern object creation
        Pattern pattern = Pattern.compile("<h1>(/S+)</h1>");
       
        // now the above compiled pattern will be checked
        // for its availability in target string
        Matcher matcher = pattern.matcher(str);
 
        if (matcher.find()) {
            String instr = matcher.group(1);
            System.out.println(instr);
        }
    }
}


 Output:

GeeksforGeeks

Time complexity : O(n), where n is the length of the input string “str”. 

Space complexity :O(1), as the space used by the program remains constant regardless of the size of the input string.


Last Updated : 31 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads