How to validate Indian Passport number using Regular Expression | Java program to validate passport number of India using regular expression ; Function to validate passport number of India using regular expression ; Regex to check valid . passport number of India ; 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 isValidPassportNo ( String str ) { String regex = " ^ [ A - PR - WYa - pr - wy ] [1-9 ] \\ d " + " \\ s ? \\ d { 4 } [ 1-9 ] $ " ; 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 = " A21 ▁ 90457" ; System . out . println ( isValidPassportNo ( str1 ) ) ; String str2 = " A0296457" ; System . out . println ( isValidPassportNo ( str2 ) ) ; String str3 = " Q2096453" ; System . out . println ( isValidPassportNo ( str3 ) ) ; String str4 = "12096457" ; System . out . println ( isValidPassportNo ( str4 ) ) ; String str5 = " A209645704" ; System . out . println ( isValidPassportNo ( str5 ) ) ; } }