How to validate HTML tag using Regular Expression - GeeksforGeeks

04 Feb, 2021
Given string str, the task is to check whether it is a valid HTML tag or not by using Regular Expression.The valid HTML tag must satisfy the following conditions: 
It should start with an opening tag (<).It should be followed by a double quotes string or single quotes string.It should not allow one double quotes string, one single quotes string or a closing tag (>) without single or double quotes enclosed.It should end with a closing tag (>).
It should start with an opening tag (<).
It should be followed by a double quotes string or single quotes string.
It should not allow one double quotes string, one single quotes string or a closing tag (>) without single or double quotes enclosed.
It should end with a closing tag (>).
Examples: 
Input: str = “<input value = ‘>’>”; Output: true Explanation: The given string satisfies all the above mentioned conditions.Input: str = “<br/>”; Output: true Explanation: The given string satisfies all the above mentioned conditions.Input: str = “br/>”; Output: false Explanation: The given string doesn’t starts with an opening tag “<“. Therefore, it is not a valid HTML tag.Input: str = “<‘br/>”; Output: false Explanation: The given string has one single quotes string that is not allowed. Therefore, it is not a valid HTML tag.Input: str = “<input value => >”; Output: false Explanation: The given string has a closing tag (>) without single or double quotes enclosed that is not allowed. Therefore, it is not a valid HTML tag.
Approach: The idea is to use Regular Expression to solve this problem. The following steps can be followed to compute the answer. 
Get the String.
Create a regular expression to check valid HTML tag as mentioned below:  
regex = “<(“[^”]*”|'[^’]*’|[^'”>])*>”; 
Where: < represents the string should start with an opening tag (<).( represents the starting of the group.“[^”]*” represents the string should allow double quotes enclosed string.| represents or.‘[^’]*‘ represents the string should allow single quotes enclosed string.| represents or.[^'”>] represents the string should not contain one single quote, double quotes, and “>”.) represents the ending of the group.* represents 0 or more.> represents the string should end with a closing tag (>).
< represents the string should start with an opening tag (<).
( represents the starting of the group.
“[^”]*” represents the string should allow double quotes enclosed string.
| represents or.
‘[^’]*‘ represents the string should allow single quotes enclosed string.
| represents or.
[^'”>] represents the string should not contain one single quote, double quotes, and “>”.
) represents the ending of the group.
* represents 0 or more.
> represents the string should end with a closing tag (>).
Match the given string with the regular expression. In Java, this can be done by using Pattern.matcher().
Return true if the string matches with the given regular expression, else return false.
Below is the implementation of the above approach:  
C++
Java
Python3
// C++ program to validate the// HTML tag using Regular Expression#include <iostream>#include <regex>using namespace std; // Function to validate the HTML tag.bool isValidHTMLTag(string str){   // Regex to check valid HTML tag.  const regex pattern("<(\"[^\"]*\"|'[^']*'|[^'\">])*>");   // If the HTML tag  // is empty return false  if (str.empty())  {     return false;  }   // Return true if the HTML tag  // matched the ReGex  if(regex_match(str, pattern))  {    return true;  }  else  {    return false;  }} // Driver Codeint main(){     // Test Case 1:  string str1 = "<input value = '>'>";  cout << isValidHTMLTag(str1) << endl;   // Test Case 2:  string str2 = "<br/>";  cout << isValidHTMLTag(str2) << endl;   // Test Case 3:  string str3 = "br/>";  cout << isValidHTMLTag(str3) << endl;   // Test Case 4:  string str4 = "<'br/>";  cout << isValidHTMLTag(str4) << endl;   // Test Case 5:  string str5 = "<input value => >";  cout << isValidHTMLTag(str5) << endl;   return 0;} // This code is contributed by yuvraj_chandra
// Java program to validate// HTML tag using regex. import java.util.regex.*; class GFG {     // Function to validate    // HTML tag using regex.    public static boolean    isValidHTMLTag(String str)    {        // Regex to check valid HTML tag.        String regex            = "<(\"[^\"]*\"|'[^']*'|[^'\">])*>";         // Compile the ReGex        Pattern p = Pattern.compile(regex);         // If the string is empty        // return false        if (str == null) {            return false;        }         // Find match between given string        // and regular expression        // using Pattern.matcher()        Matcher m = p.matcher(str);         // Return if the string        // matched the ReGex        return m.matches();    }     // Driver Code.    public static void main(String args[])    {         // Test Case 1:        String str1 = "<input value = '>'>";        System.out.println(isValidHTMLTag(str1));         // Test Case 2:        String str2 = "<br/>";        System.out.println(isValidHTMLTag(str2));         // Test Case 3:        String str3 = "br/>";        System.out.println(isValidHTMLTag(str3));         // Test Case 4:        String str4 = "<'br/>";        System.out.println(isValidHTMLTag(str4));         // Test Case 5:        String str5 = "<input value => >";        System.out.println(isValidHTMLTag(str5));    }}
# Python3 program to validate# HTML tag using regex.  # using regular expressionimport re # Function to validate# HTML tag using regex.def isValidHTMLTag(str):     # Regex to check valid    # HTML tag using regex.    regex = "<(\"[^\"]*\"|'[^']*'|[^'\">])*>"         # Compile the ReGex    p = re.compile(regex)     # If the string is empty    # return false    if (str == None):        return False     # Return if the string    # matched the ReGex    if(re.search(p, str)):        return True    else:        return False # Driver code # Test Case 1:str1 = "<input value = '>'>"print(isValidHTMLTag(str1)) # Test Case 2:str2 = "<br/>"print(isValidHTMLTag(str2)) # Test Case 3:str3 = "br/>"print(isValidHTMLTag(str3)) # Test Case 4:str4 = "<'br/>"print(isValidHTMLTag(str4)) # This code is contributed by avanitrachhadiya2155
true
true
false
false
false
 
avanitrachhadiya2155
yuvraj_chandra
CPP-regex
java-regular-expression
regular-expression
HTML
Pattern Searching
Strings
Strings
Pattern Searching
HTML
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
REST API (Introduction)
Design a web page using HTML and CSS
Form validation using jQuery
How to place text on image using HTML and CSS?
How to auto-resize an image to fit a div container using CSS?
KMP Algorithm for Pattern Searching
Rabin-Karp Algorithm for Pattern Searching
Check if a string is substring of another
Naive algorithm for Pattern Searching
Boyer Moore Algorithm for Pattern Searching