2015-05-27 13 views
8

Ho visto this question e this blog per l'espressione regolare PAN. [A-Z]{5}[0-9]{4}[A-Z]{1}. Ma la mia domanda è un po 'più estesa di quella.Espressione regolare che convalida il numero di tessera PAN

In un numero di carta di 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. 

Voglio l'espressione regolare da verificare sulla base di questo. Da quando ho ricevuto il nome della persona o l'organizzazione in un altro EditText, ho dovuto verificare ulteriormente la quarta e la quinta lettera.

Si scopre essere [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}

Io non sono in grado di capire come che qualcosa deve essere scritto.

Programmare, è possibile, someone has done it in rails ma può essere eseguito tramite regex? Come?

+1

hai provato (P | [C, H, F, a, T, B, L, J, G]) –

+0

non può in grado di comprendere il 3 ° punto. Quanti caratteri sono presenti? –

+0

@AvinashRaj se il quarto carattere nel numero della carta PAN è P, allora il quinto carattere sarà la prima lettera del tuo cognome, ad es. Se sei Avinash Raj, il 4 ° personaggio del tuo numero di Pancard sarà P per Personale e il quinto personaggio sarà R di Raj. Considerando che se la carta PAN appartiene a un'organizzazione, diciamo un'azienda quindi il quarto carattere è C per la società e il quinto carattere è la prima lettera del nome della società, ad esempio, se esiste una società chiamata "Microsoft Corporation", quindi il quarto carattere sarebbe "C" per Company e il quinto carattere sarebbe "M" per Microsoft. – inquisitive

risposta

4

L'espressione regolare che è possibile utilizzare con matches() viene formata in base all'input aggiuntivo degli utenti e gli elementi di look-behind controllano il quarto carattere precedente. Se il 4 ° lettera è P, controlliamo per la prima lettera del cognome, e se il 4 ° lettera non è P, controlliamo la prima lettera del nome di entità:

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