How to extract an HTML tag from a String using regex in Java?


The java.util.regex package of java provides various classes to find particular patterns in character sequences.

The pattern class of this package is a compiled representation of a regular expression. To match a regular expression with a String this class provides two methods namely −

  • compile() − This method accepts a String representing a regular expression and returns an object of the class Pattern.

  • matcher() − This method accepts a String value and creates a matcher object which matches the given String to the pattern represented by the current pattern object.

The Matcher class of java.util.regex package is an engine that performs match operations. To find the matched value this you need to use two methods of this class namely −

  • find() − This method returns true if the match operation represented by the current object is successful else, it returns false.

  • group() − This method accepts an integer value representing a particular group and returns the sequence captured by the specified group in the match operation.

Therefore, to find an HTML tag from a String −

  • Create a Pattern object by passing the regular expression representing the required HTML tag to it as a parameter to the compile() method of the Pattern class.

  • Match it with the desired String using the matcher method() of the Pattern class.

  • Verify if a occurred using the find() method of the Matcher class.

  • In the case of a match, retrieve the matched String using the group() method of the Matcher class.

Example

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ExtractHtmlTag {
   public static void main(String[] args) {
      String str = "Welcome to Tutorialspoint";
      //Creating a pattern object
      Pattern pattern = Pattern.compile("(\\S+)");
      //Matching the compiled pattern in the String
      Matcher matcher = pattern.matcher(str);
      if (matcher.find()) {
         String result = matcher.group(1);
         System.out.println(result);
      }
   }
}

Output

Tutorialspoint
raja
Published on 10-Oct-2019 10:25:21
Advertisements