Program to remove HTML tags from a given String - GeeksforGeeks

11 Feb, 2022
Given a string str which contains some HTML tags, the task is to remove all the tags present in the given string str.Examples:  
Input: str = “<div><b>Geeks for Geeks</b></div>” Output: Geeks for GeeksInput: str = “<a href=”https://www.geeksforgeeks.org/”>GFG</a>” Output: GFG  
 
Approach: The idea is to use the Regular Expression to solve this problem. The following steps can be followed to compute the resultant string:  
Get the string.Since every HTML tags are enclosed in angular brackets(<>). Therefore use replaceAll() function in regex to replace every substring start with “<“ and ends with “>” to empty string.The function is used as:  
Get the string.
Since every HTML tags are enclosed in angular brackets(<>). Therefore use replaceAll() function in regex to replace every substring start with “<“ and ends with “>” to empty string.
The function is used as:  
String str;
str.replaceAll("\\", "");
Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ program for the above approach#include <iostream>#include <regex>using namespace std; // Function to remove the HTML tags// from the given stringvoid RemoveHTMLTags(string s){  const regex pattern("\\<.*?\\>");   // Use regex_replace function in regex  // to erase every tags enclosed in <>  s = regex_replace(s, pattern, "");   // Print string after removing tags  cout << s;   return ;} // Driver Codeint main(){     // Given String  string str = "<div><b>Geeks for Geeks</b></div>";   // Function call to print the  // HTML string after removing tags  RemoveHTMLTags(str) ;   return 0;} // This code is contributed by yuvraj_chandra
// Java program for the above approach class GFG {     // Function to remove the HTML tags    // from the given tags    static void RemoveHTMLTags(String str)    {         // Use replaceAll function in regex        // to erase every tags enclosed in <>        str = str.replaceAll("\\<.*?\\>", "");         // Print string after removing tags        System.out.println(str);    }     // Driver Code    public static void main(String[] args)    {        String str;         // Given String        str = "<div><b>Geeks for Geeks</b></div>";         // Function call to print the        // HTML string after removing tags        RemoveHTMLTags(str);    }}
# Python3 program for the# above approachimport re # Function to remove the HTML tags# from the given tagsdef RemoveHTMLTags(strr):         # Print string after removing tags    print(re.compile(r'<[^>]+>').sub('', strr))     # Driver codeif __name__=='__main__':         # Given String    strr = "<div><b>Geeks for Geeks</b></div>"         # Function call to print the HTML    # string after removing tags    RemoveHTMLTags(strr);     # This code is contributed by vikas_g
// C# program for the above approachusing System; class GFG{ // Function to remove the HTML tags// from the given tagsstatic void RemoveHTMLTags(String str){         // Use replaceAll function in regex    // to erase every tags enclosed in <>    // str = Regex.Replace(str, "<.*?>", String.Empty)    System.Text.RegularExpressions.Regex rx =    new System.Text.RegularExpressions.Regex("<[^>]*>");         str = rx.Replace(str, "");     // Print string after removing tags    Console.WriteLine(str);} // Driver codepublic static void Main(String []args){    String str;     // Given String    str = "<div><b>Geeks for Geeks</b></div>";     // Function call to print the    // HTML string after removing tags    RemoveHTMLTags(str);}} // This code is contributed by vikas_g
<script> // JavaScript program for the above approach  // Function to remove the HTML tags// from the given stringfunction RemoveHTMLTags(s) {    const pattern = new RegExp("\\<.*?\\>");     // Use regex_replace function in regex    // to erase every tags enclosed in <>    s = new String(s).replace(pattern, "");     // Print string after removing tags    document.write(s);     return;} // Driver Code  // Given Stringlet str = "<div><b>Geeks for Geeks</b></div>"; // Function call to print the// HTML string after removing tagsRemoveHTMLTags(str); </script>
Geeks for Geeks
 
vikas_g
yuvraj_chandra
_saurabh_jaiswal
rkbhola5
CPP-regex
HTML-Tags
java-regular-expression
regular-expression
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Top 50 String Coding Problems for Interviews
Hill Cipher
Naive algorithm for Pattern Searching
Vigenère Cipher
Convert character array to string in C++
Reverse words in a given String in Python
sprintf() in C
String class in Java | Set 1
How to Append a Character to a String in C
Print all the duplicates in the input string