2012-04-03 16 views
5

Sto provando a utilizzare .NET Regex per convalidare il formato di input di una stringa. La stringa può essere di formatoProblema Espressione regolare semplice (Regex) (.net)

single digit 0-9 followed by 
single letter A-Z OR 07 OR 03 or AA followed by 
two letters A-Z 

Così 0AAA, 107ZF, 503GH, 0AAAA sono tutte valide. La stringa con cui costruisco la mia Regex è la seguente:

"([0-9]{1})" + 
    "((03$)|(07$)|(AA$)|[A-Z]{1})" + 
    "([A-Z]{2})" 

Eppure questo non convalida le stringhe in cui il secondo termine è uno dei 03, 07 o AA. Mentre eseguivo il debug, rimuovevo il terzo termine dalla stringa usata per costruire l'espressione regolare e trovavo che le stringhe di input del modulo 103, 507, 6AA WAND convalidavano .......

Qualche idea per cui, quando poi rimettere il terzo termine nel Regex, le stringhe di input come 1AAGM non corrispondono?

Grazie Tom

+0

FYI, ho trovato davvero questo strumento utile per testare regex http://gskinner.com/RegExr/ – michele

risposta

9

Questo in quanto l'espressione richiede le corde con 03, 07 e AA per finire proprio lì ($ corrisponde alla fine di ingresso). Rimuovi lo $ da queste sottoespressioni e spostalo alla fine dell'espressione.

"^[0-9](03|07|AA|[A-Z])[A-Z]{2}$" 
+0

+1, mentre la mia risposta spiega cosa stava facendo e ha offerto una soluzione. Questa risposta assicura che 107ZFD, ad esempio, non si convalida, mentre il mio corrisponderà alla porzione 107ZF della stringa. Non sono sicuro di quale sia quello che vuole, ma questa è anche una buona risposta. – Matt

4

Credo che questo sia perché si sta utilizzando il "$" nella regex, il che significa che in questo caso far valere la posizione alla fine di una linea (alla fine della stringa o prima di un carattere di interruzione di riga). Rimuovilo e dovrebbe funzionare. Da Regex Buddy, ecco quello che stavi facendo:

([0-9]{1})((03$)|(07$)|(AA$)|[A-Z]{1})([A-Z]{2}) 
Options:^and $ match at line breaks 

Match the regular expression below and capture its match into backreference number 1 «([0-9]{1})» 
    Match a single character in the range between “0” and “9” «[0-9]{1}» 
     Exactly 1 times «{1}» 
Match the regular expression below and capture its match into backreference number 2 «((03$)|(07$)|(AA$)|[A-Z]{1})» 
    Match either the regular expression below (attempting the next alternative only if this one fails) «(03$)» 
     Match the regular expression below and capture its match into backreference number 3 «(03$)» 
     Match the characters “03” literally «03» 
     Assert position at the end of a line (at the end of the string or before a line break character) «$» 
    Or match regular expression number 2 below (attempting the next alternative only if this one fails) «(07$)» 
     Match the regular expression below and capture its match into backreference number 4 «(07$)» 
     Match the characters “07” literally «07» 
     Assert position at the end of a line (at the end of the string or before a line break character) «$» 
    Or match regular expression number 3 below (attempting the next alternative only if this one fails) «(AA$)» 
     Match the regular expression below and capture its match into backreference number 5 «(AA$)» 
     Match the characters “AA” literally «AA» 
     Assert position at the end of a line (at the end of the string or before a line break character) «$» 
    Or match regular expression number 4 below (the entire group fails if this one fails to match) «[A-Z]{1}» 
     Match a single character in the range between “A” and “Z” «[A-Z]{1}» 
     Exactly 1 times «{1}» 
Match the regular expression below and capture its match into backreference number 6 «([A-Z]{2})» 
    Match a single character in the range between “A” and “Z” «[A-Z]{2}» 
     Exactly 2 times «{2}» 

seguita dalla versione riveduta:

([0-9]{1})((03)|(07)|(AA)|[A-Z]{1})([A-Z]{2}) 
Options:^and $ match at line breaks 

Match the regular expression below and capture its match into backreference number 1 «([0-9]{1})» 
    Match a single character in the range between “0” and “9” «[0-9]{1}» 
     Exactly 1 times «{1}» 
Match the regular expression below and capture its match into backreference number 2 «((03)|(07)|(AA)|[A-Z]{1})» 
    Match either the regular expression below (attempting the next alternative only if this one fails) «(03)» 
     Match the regular expression below and capture its match into backreference number 3 «(03)» 
     Match the characters “03” literally «03» 
    Or match regular expression number 2 below (attempting the next alternative only if this one fails) «(07)» 
     Match the regular expression below and capture its match into backreference number 4 «(07)» 
     Match the characters “07” literally «07» 
    Or match regular expression number 3 below (attempting the next alternative only if this one fails) «(AA)» 
     Match the regular expression below and capture its match into backreference number 5 «(AA)» 
     Match the characters “AA” literally «AA» 
    Or match regular expression number 4 below (the entire group fails if this one fails to match) «[A-Z]{1}» 
     Match a single character in the range between “A” and “Z” «[A-Z]{1}» 
     Exactly 1 times «{1}» 
Match the regular expression below and capture its match into backreference number 6 «([A-Z]{2})» 
    Match a single character in the range between “A” and “Z” «[A-Z]{2}» 
     Exactly 2 times «{2}» 
+0

D'accordo, il $ dovrebbe essere il problema. – jessehouwing