How to validate Indian driving license number using Regular Expression | Java program to validate Indian driving license number using regular expression ; Function to validate Indian driving license number using regular expression ; Regex to check valid Indian driving license number ; 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 isValidLicenseNo ( String str ) { String regex = " ^ ( ( [ A - Z ] { 2 } [ 0-9 ] { 2 } ) " + " ( ▁ ) | ( [ A - Z ] { 2 } - [0-9 ] " + " { 2 } ) ) ( (19 ▁ 20 ) [ 0-9 ] " + " [ 0-9 ] ) [0-9 ] { 7 } $ " ; 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 = " HR - 0619850034761" ; System . out . println ( isValidLicenseNo ( str1 ) ) ; String str2 = " UP14 ▁ 20160034761" ; System . out . println ( isValidLicenseNo ( str2 ) ) ; String str3 = "12HR - 37200602347" ; System . out . println ( isValidLicenseNo ( str3 ) ) ; String str4 = " MH27 ▁ 30123476102" ; System . out . println ( isValidLicenseNo ( str4 ) ) ; String str5 = " GJ - 2420180" ; System . out . println ( isValidLicenseNo ( str5 ) ) ; } }