正则表达式验证PAN卡号

时间:2015-05-27 04:47:07

标签: android regex

我看过PAN正则表达式的this questionthis blog[A-Z]{5}[0-9]{4}[A-Z]{1}。但我的问题比那更加广泛。

在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但可以通过正则表达式完成吗?怎么样?

2 个答案:

答案 0 :(得分:5)

可以与P一起使用的正则表达式是根据用户的附加输入形成的,并且后面检查前面的第4个字符。如果第4个字母是P,我们会检查姓氏中的第一个字母,如果第4个字母不是String rx = "[A-Z]{3}([CHFATBLJGP])(?:(?<=P)" + c1 + "|(?<!P)" + c2 + ")[0-9]{4}[A-Z]"; ,我们会检查实体名称中的第一个字母:

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

Sample code

DisplayHeight

答案 1 :(得分: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();
}