正则expression式validationPAN卡号

我已经看到了这个问题 , 这个博客的PAN正则expression式[AZ]{5}[0-9]{4}[AZ]{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. 

我想要正则expression式在此基础上进行检查。 由于我得到了该人的姓名,或者在另一个EditText中获得了该组织的名字,所以我需要进一步validation第4个和第5个字母。

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

我无法弄清楚如何写东西

以编程方式,它可以完成, 有人在轨道上做到了,但可以通过正则expression式吗? 怎么样?

Solutions Collecting From Web of "正则expression式validationPAN卡号"

matches()可以使用的正则expression式是基于用户的附加input而形成的,而look-behinds会检查前面的第四个字符。 如果第四个字母是P ,我们检查姓氏中的第一个字母,如果第四个字母不是P ,我们检查实体名称中的第一个字母:

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

示例代码 :

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

在这里输入图像说明

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