java

How to remove text between tags using Regex in Java

In this tutorial, we are going to see how to remove text between tags using Regex in Java. Regular Expressions or Regex is an API for defining patterns that can be used to find, manipulate, and edit a string in Java. Regex is widely used to define constraints. Regular expressions are provided under java.util package.
 

Java Program to remove text between tags using Regex:

The following program shows how to remove the text between <b> tags.

import java.util.regex.*;

public class Main 
{
    public static void main(String[]args) 
    {
        String str = "Welcome To StackHowTo!";
        str = str.replaceAll("(?s).*?", "");
      
        System.out.println(str); 
    }
}

Output:

Welcome To !

 

 

Explanation:
  • (?s) it’s the DOTALL modifier that activates dotall mode. By default, the “.” In regular expressions matches all characters except line breaks. In other words, when you activate DOTALL mode, the “.“ Matches all characters, including line breaks.
  • <b> Corresponds to the opening tag.
  • .*? Matches any character between the <b> and </b> tag. ? after * tells the regex engine to make the shortest match.
  • Finally, the corresponding characters are replaced by an empty string.
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *