Regular Expression Confirming PAN Card Number

I saw this question and this blog for regular PAN expression. [AZ]{5}[0-9]{4}[AZ]{1} . But my question is a little wider than that.

In the PAN card number:

 1) The first three letters are sequence of alphabets from AAA to zzz 2) The fourth character informs about the type of holder of the Card. Each assesse is unique:` C β€” Company P β€” Person H β€” HUF(Hindu Undivided Family) F β€” Firm A β€” Association of Persons (AOP) T β€” AOP (Trust) B β€” Body of Individuals (BOI) L β€” Local Authority J β€” Artificial Judicial Person G β€” Government 3) The fifth character of the PAN is the first character (a) of the surname / last name of the person, in the case of a "Personal" PAN card, where the fourth character is "P" or (b) of the name of the Entity/ Trust/ Society/ Organisation in the case of Company/ HUF/ Firm/ AOP/ BOI/ Local Authority/ Artificial Jurdical Person/ Govt, where the fourth character is "C","H","F","A","T","B","L","J","G". 4) The last character is a alphabetic check digit. 

I want the regex to be checked based on this. Since I get the name of the person or organization in another EditText, I needed to additionally check the 4th and 5th letter.

It turns out [AZ]{3}[C,H,F,A,T,B,L,J,G,P]{1}**something for the fifth character**[0-9]{4}[AZ]{1}

I can’t understand how this something should be written.

Programmatically, this can be done, someone did it in rails , but can this be done using regular expressions? How?

+8
android regex
source share
2 answers

The regular expression that you can use with matches() is formed on the basis of additional input from users, and checking the reverse for the previous 4th character. If the 4th letter is P , we check the first letter in the last name, and if the 4th letter is not P , we check the first letter in the name of the object:

 String rx = "[AZ]{3}([CHFATBLJGP])(?:(?<=P)" + c1 + "|(?<!P)" + c2 + ")[0-9]{4}[AZ]"; 

Code example :

 String c1 = "S"; // First letter in surname coming from the EditText (with P before) String c2 = "F"; // First letter in name coming from another EditText (not with P before) String pan = "AWSPS1234Z"; // true System.out.println(pan.matches("[AZ]{3}([CHFATBLJGP])(?:(?<=P)" + c1 + "|(?<!P)" + c2 + ")[0-9]{4}[AZ]")); pan = "AWSCF1234Z"; // true System.out.println(pan.matches("[AZ]{3}([CHFATBLJGP])(?:(?<=P)" + c1 + "|(?<!P)" + c2 + ")[0-9]{4}[AZ]")); pan = "AWSCS1234Z"; // false System.out.println(pan.matches("[AZ]{3}([CHFATBLJGP])(?:(?<=P)" + c1 + "|(?<!P)" + c2 + ")[0-9]{4}[AZ]")); 
+4
source share

enter image description here

 Pan= edittextPan.getText().toString().trim(); Pattern pattern = Pattern.compile("[AZ]{5}[0-9]{4}[AZ]{1}"); Matcher matcher = pattern .matcher(Pan); if (matcher .matches()) { Toast.makeText(getApplicationContext(), Pan+" is Matching", Toast.LENGTH_LONG).show(); } else { Toast.makeText(getApplicationContext(), Pan+" is Not Matching", Toast.LENGTH_LONG).show(); } 
+1
source share

All Articles