15

I have seen this question and this blog for the PAN regex. [A-Z]{5}[0-9]{4}[A-Z]{1}. But my question is a little bit more extended than that.

In a 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 checking on the basis of that. Since I get Name of the person, or the Organization in another EditText, I needed to further verify the 4th and 5th letter.

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

I'm not able to figure out how that something has to be written.

Programmatically, it can be done, someone has done it in rails but can it be done through regex? How?

Community
  • 1
  • 1
inquisitive
  • 3,738
  • 6
  • 30
  • 56
  • 1
    did you try (P|[C,H,F,A,T,B,L,J,G]) – Uma Kanth May 27 '15 at 04:50
  • can't able to understand the 3rd point. How many chars present? – Avinash Raj May 27 '15 at 04:51
  • @AvinashRaj if the fourth character in the PAN card number is P, then the fifth character would be first letter of your surname, For eg. If you are Avinash Raj, your pancard number's 4th character would be P for Personal and fifth character would be R of Raj. Whereas if the PAN card belongs to an Organization, let say a company then fourth character is C for Company and fifth character is first letter of the name of the company, for eg, if there is a company called, "Microsoft Corporation", then the fourth character would be "C" for Company and fifth character would be "M" for Microsoft. – inquisitive May 27 '15 at 04:55
  • @UmaLakshmiKanth yeah, that is more correct, how would i then approach for fifth character? let say I have `char c1='R', c2='M'` c1 first letter of last name, c2 first letter of the name. how would I then create the regex? – inquisitive May 27 '15 at 04:59
  • @Inquisitive: Does [this](https://ideone.com/KSIJfx) solve the issue? – Wiktor Stribiżew May 27 '15 at 06:55
  • 1
    @stribizhev this is not the actual answer but i think, we need to add your code plus look at the comment of Uma Lakshmi Kanth, so that even the deicison to match either c1 or c2 is also decided on the basis of fourth character – inquisitive May 27 '15 at 07:28

2 Answers2

9

The regex you can use with matches() is formed based on the additional input from the users, and look-behinds check for the preceding 4th character. If the 4th letter is P, we check for the first letter in the surname, and if the 4th letter is not P, we check the first letter in the entity name:

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

Sample code:

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("[A-Z]{3}([CHFATBLJGP])(?:(?<=P)" + c1 + "|(?<!P)" + c2 + ")[0-9]{4}[A-Z]"));
pan = "AWSCF1234Z"; // true
System.out.println(pan.matches("[A-Z]{3}([CHFATBLJGP])(?:(?<=P)" + c1 + "|(?<!P)" + c2 + ")[0-9]{4}[A-Z]"));
pan = "AWSCS1234Z"; // false
System.out.println(pan.matches("[A-Z]{3}([CHFATBLJGP])(?:(?<=P)" + c1 + "|(?<!P)" + c2 + ")[0-9]{4}[A-Z]"));
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
5

enter image description here

Pan= edittextPan.getText().toString().trim();

Pattern pattern = Pattern.compile("[A-Z]{5}[0-9]{4}[A-Z]{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();
}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Dhina k
  • 1,481
  • 18
  • 24