2015-05-27 20 views
8

我看过PAN103正常表达式的this questionthis blog[A-Z]{5}[0-9]{4}[A-Z]{1}。但是我的问题比这个扩大了一点点。验证PAN卡号的正则表达式

在PAN卡号码:

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. 

我想正则表达式要在此基础上检查。由于我得到了该人的姓名,或者在另一个EditText中获得了该组织名称,所以我需要进一步验证第4封和第5封信。

它原来是[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}

我无法弄清楚如何说东西已被写入。

以编程方式,它可以完成,someone has done it in rails但它可以通过正则表达式吗?怎么样?

+1

做尝试(P | [C,H,F,A,T,B,L,J,G]) –

+0

不能能够理解第三点。有多少个字符? –

+0

@AvinashRaj如果PAN卡号中的第四个字符是P,那么第五个字符将是您姓的第一个字母,例如。如果你是Avinash Raj,你的pancard数字的第4个字符将是P个人,第5个字符将是Raj的R.而如果PAN卡属于一个组织,比如说一家公司,那么第四个字符就是公司的C,第五个字母是公司名称的第一个字母,例如,如果有一家公司叫做“微软公司”,那么公司的第四个字符是“C”,第五个字符是微软的“M”。 – inquisitive

回答

4

您可以使用matches()的正则表达式是基于用户的额外输入而形成的,并且look-behinds检查前面的第4个字符。如果第四个字母是P,我们为您在姓氏的第一个字母,如果第四个字母不是P,我们检查的实体名称的第一个字母:

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]")); 
1

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(); 
}