Open In App

How to validate HTML tag using Regular Expression

Last Updated : 03 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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: 

  1. It should start with an opening tag (<).
  2. It should be followed by a double quotes string or single quotes string.
  3. It should not allow one double quotes string, one single quotes string or a closing tag (>) without single or double quotes enclosed.
  4. 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 (>).
  • 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++




// 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 Code
int main()
{
   
  // Test Case 1:
  string str1 = "<input value = '>'>";
  cout << ((isValidHTMLTag(str1)) ? "true" : "false") << endl;
 
  // Test Case 2:
  string str2 = "<br/>";
  cout << ((isValidHTMLTag(str2)) ? "true" : "false") << endl;
 
  // Test Case 3:
  string str3 = "br/>";
  cout << ((isValidHTMLTag(str3)) ? "true" : "false")<< endl;
 
  // Test Case 4:
  string str4 = "<'br/>";
  cout << ((isValidHTMLTag(str4))? "true" : "false") << endl;
 
 
  return 0;
}
 
// This code is contributed by yuvraj_chandra


Java




// 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));
 
    }
}


Python3




# Python3 program to validate
# HTML tag using regex.  
# using regular expression
import 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


C#




// C# program to validate
// HTML tag using regex.
 
using System;
using System.Text.RegularExpressions;
 
class GFG {
 
  // Function to validate
  // HTML tag using regex.
  public static bool
    isValidHTMLTag(string str)
  {
    // Regex to check valid HTML tag.
    string regex
      = "<(\"[^\"]*\"|'[^']*'|[^'\">])*>";
 
    // Compile the ReGex
    Regex p = new Regex(regex);
 
    // If the string is empty
    // return false
    if (str == null) {
      return false;
    }
 
    // Find match between given string
    // and regular expression
    // using Pattern.matcher()
    Match m = p.Match(str);
 
    // Return if the string
    // matched the ReGex
    return m.Success;
  }
 
  // Driver Code.
  public static void Main()
  {
 
    // Test Case 1:
    string str1 = "<input value = '>'>";
    Console.WriteLine(isValidHTMLTag(str1));
 
    // Test Case 2:
    string str2 = "<br/>";
    Console.WriteLine(isValidHTMLTag(str2));
 
    // Test Case 3:
    string str3 = "br/>";
    Console.WriteLine(isValidHTMLTag(str3));
 
    // Test Case 4:
    string str4 = "<'br/>";
    Console.WriteLine(isValidHTMLTag(str4));
 
  }
}
 
// This code is contributed by Aman Kumar.


Javascript




// Javascript program to validate
// HTML tag using regex.
  
    // Function to validate
    // HTML tag using regex.
    function isValidHTMLTag(str)
    {
        // Regex to check valid HTML tag.
        let regex = new RegExp(/<(\"[^\"]*\"|'[^']*'|[^'\">])*>/);
 
        // If the string is empty
        // return false
        if (str == null) {
            return "false";
        }
  
        // Find match between given string
        // and regular expression
        if (regex.test(str) == true) {
            return "true";
        }
        else {
            return "false";
        }
    }
  
    // Driver Code.
  
        // Test Case 1:
        let str1 = "<input value = '>'>";
        console.log(isValidHTMLTag(str1)+"<br>");
  
        // Test Case 2:
        let str2 = "<br/>";
        console.log(isValidHTMLTag(str2)+"<br>");
  
        // Test Case 3:
        let str3 = "br/>";
        console.log(isValidHTMLTag(str3)+"<br>");
  
        // Test Case 4:
        let str4 = "<'br/>";
        console.log(isValidHTMLTag(str4)+"<br>");
         
   // This code is contributed by Utkarsh Kumar


Output: 

true
true
false
false
false

 

Time Complexity: O(N) for each test case, where N is the length of the given string. 
Auxiliary Space: O(1)



Similar Reads

How to validate MAC address using Regular Expression
Given string str, the task is to check whether the given string is a valid MAC address or not by using Regular Expression. A valid MAC address must satisfy the following conditions: It must contain 12 hexadecimal digits.One way to represent them is to form six pairs of the characters separated with a hyphen (-) or colon(:). For example, 01-23-45-67
6 min read
How to validate Indian driving license number using Regular Expression
Given string str, the task is to check whether the given string is a valid Indian driving license number or not by using Regular Expression.The valid Indian driving license number must satisfy the following conditions: It should be 16 characters long (including space or hyphen (-)).The driving license number can be entered in any of the following f
7 min read
How to validate CVV number using Regular Expression
Given string str, the task is to check whether it is a valid CVV (Card Verification Value) number or not by using Regular Expression. The valid CVV (Card Verification Value) number must satisfy the following conditions: It should have 3 or 4 digits.It should have a digit between 0-9.It should not have any alphabet or special characters. Examples: I
5 min read
How to validate URL using regular expression in JavaScript?
Given a URL, the task is to validate it. Here we are going to declare a URL valid or Invalid by matching it with the JavaScript RegExp. We're going to discuss a few methods. Example 1: This example validates the URL = 'https://www.geeksforgeeks.org' by using Regular Expression. C/C++ Code &lt;h1 style=&quot;color:green;&quot;&gt; GeeksForGeeks &lt;
2 min read
How to validate SSN (Social Security Number) using Regular Expression
Given string str, the task is to check whether the given string is valid SSN (Social Security Number) or not by using Regular Expression. The valid SSN (Social Security Number) must satisfy the following conditions: It should have 9 digits.It should be divided into 3 parts by hyphen (-).The first part should have 3 digits and should not be 000, 666
6 min read
Validate Corporate Identification Number (CIN) using Regular Expression
Given some Corporate Identification Number, the task is to check if they are valid or not using regular expressions. Rules for the valid CIN are: CIN is a 21 digits alpha-numeric code.It starts with either alphabet letter U or L.Next five characters are reserved for digits (0-9).Next two places are occupied by alphabet letters(A-Z-a-z).Next four pl
6 min read
How to validate identifier using Regular Expression in Java
Given a string str, the task is to check whether the string is a valid identifier or not using the Regular Expression. The valid rules for defining Java identifiers are: It must start with either lower case alphabet[a-z] or upper case alphabet[A-Z] or underscore(_) or a dollar sign($).It should be a single word, the white spaces are not allowed.It
2 min read
How to validate time in 12-hour format using Regular Expression
Given a string str, the task is to check whether the string is valid time in 12-hour format or not by using Regular Expression. The valid time in a 12-hour format must satisfy the following conditions: It should start from 1, 2, ... 9 or 10, 11, 12.It should be followed by a colon(:).It should be followed by two digits between 00 to 59.It should on
6 min read
How to validate PAN Card number using Regular Expression
Given string str of alphanumeric characters, the task is to check whether the string is a valid PAN (Permanent Account Number) Card number or not by using Regular Expression.The valid PAN Card number must satisfy the following conditions: It should be ten characters long.The first five characters should be any upper case alphabets.The next four-cha
6 min read
How to validate time in 24-hour format using Regular Expression
Given a string str, the task is to check whether the string is valid time in 24-hour format or not by using Regular Expression. The valid time in the 24-hour format must satisfy the following conditions. It should start from 0-23 or 00-23.It should be followed by a ':'(colon).It should be followed by two digits from 00 to 59.It should not end with
6 min read