How to validate HTML tag using Regular Expression | Python3 program to validate HTML tag using regex . using regular expression ; Function to validate HTML tag using regex . ; Regex to check valid HTML tag using regex . ; Compile the ReGex ; If the string is empty return false ; Return if the string matched the ReGex ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 :
import re NEW_LINE def isValidHTMLTag ( str ) : NEW_LINE INDENT regex = " < ( \ " [ ^ \ " ] * \ " ▁ ' [ ^ ' ] * ' ▁ [ ^ ' \ " > ] ) * > " NEW_LINE p = re . compile ( regex ) NEW_LINE if ( str == None ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( re . search ( p , str ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT str1 = " < input ▁ value ▁ = ▁ ' > ' > " NEW_LINE print ( isValidHTMLTag ( str1 ) ) NEW_LINE str2 = " < br / > " NEW_LINE print ( isValidHTMLTag ( str2 ) ) NEW_LINE str3 = " br / > " NEW_LINE print ( isValidHTMLTag ( str3 ) ) NEW_LINE str4 = " < ' br / > " NEW_LINE print ( isValidHTMLTag ( str4 ) ) NEW_LINE