Java Program to Extract an HTML Tag from a String using RegEx
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:
- 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.
- 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
Import necessary library and classes – java.util.regex.Matcher, java.util.regex.Pattern;
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.
Using the matcher() function match it with the string from which we want to detect the HTML tags.
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.
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.
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.
Please Login to comment...