正则表达式验证PAN卡号

inq*_*ive 11 regex android

我已经看到了这个问题这个博客的PAN正则表达式.[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.
Run Code Online (Sandbox Code Playgroud)

我希望正则表达式在此基础上进行检查.由于我在另一个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}

我无法弄清楚如何编写这些东西.

以编程方式,它可以完成,有人在rails中完成它但可以通过正则表达式完成吗?怎么样?

Wik*_*żew 6

您可以使用的正则表达式matches()是根据用户的附加输入形成的,并且后面检查前面的第4个字符.如果是第4个字母P,我们检查姓氏中的第一个字母,如果第4个字母不是P,我们检查实体名称中的第一个字母:

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

示例代码:

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]"));
Run Code Online (Sandbox Code Playgroud)


Dhi*_*a k 5

在此处输入图片说明

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();
}
Run Code Online (Sandbox Code Playgroud)