How to validate HTML tag using Regular Expression | Java program to validate HTML tag using regex . ; Function to validate HTML tag using regex . ; Regex to check valid HTML tag . ; Compile the ReGex ; If the string is empty return false ; Find match between given string and regular expression using Pattern . matcher ( ) ; Return if the string matched the ReGex ; Driver Code . ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 : ; Test Case 5 :
import java . util . regex . * ; class GFG { public static boolean isValidHTMLTag ( String str ) { String regex = " < ( \" [ ^ \" ] * \" ▁ ' [ ^ ' ] * ' ▁ [ ^ ' \" > ] ) * > " ; Pattern p = Pattern . compile ( regex ) ; if ( str == null ) { return false ; } Matcher m = p . matcher ( str ) ; return m . matches ( ) ; } public static void main ( String args [ ] ) { String str1 = " < input ▁ value ▁ = ▁ ' > ' > " ; System . out . println ( isValidHTMLTag ( str1 ) ) ; String str2 = " < br / > " ; System . out . println ( isValidHTMLTag ( str2 ) ) ; String str3 = " br / > " ; System . out . println ( isValidHTMLTag ( str3 ) ) ; String str4 = " < ' br / > " ; System . out . println ( isValidHTMLTag ( str4 ) ) ; String str5 = " < input ▁ value ▁ = > ▁ > " ; System . out . println ( isValidHTMLTag ( str5 ) ) ; } }