2012-04-03 13 views
5

Estoy tratando de usar una .NET Regex para validar el formato de entrada de una cadena. La cadena puede tener el formatoProblema simple de expresión regular (Regex)

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

Así 0AAA, 107ZF, 503GH, 0AAAA son todas válidas. La cadena con la que construyo mi expresión regular es el siguiente:

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

embargo, esto no valida cadenas en las que el segundo término es uno de 03, 07 o AA. Mientras depuración, eliminé el tercer término de la cadena utilizada para construir la expresión regular, y encontré que las cadenas de entrada del formulario 103, 507, 6AA VALIDARÍAN .......

Alguna idea de por qué, cuando entonces poner el tercer término de nuevo en el Regex, las cadenas de entrada como 1AAGM no coinciden?

Gracias Tom

+0

FYI, he encontrado esta herramienta realmente útil para probar regex http://gskinner.com/RegExr/ – michele

Respuesta

9

Esto se debe a que su expresión requiere las cuerdas con 03, 07 y AA para terminar allí mismo ($ coincide con el final de la entrada). Elimine el $ de estas subexpresiones y muévalo al final de la expresión.

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

+1, mientras que mi respuesta explica lo que estaba haciendo y ofreció una solución. Esta respuesta asegura que 107ZFD, por ejemplo, no se validará, mientras que el mío coincidirá con la porción de 107ZF de la cadena. No estoy seguro de qué es lo que él quiere, pero esta también es una buena respuesta. – Matt

4

Creo que esto se debe a que está utilizando el "$" en la expresión regular, lo que significa en este caso, hacer valer la posición al final de una línea (en el extremo de la cadena o antes de un carácter de salto de línea) Quítalo y debería funcionar. De Regex Buddy, esto es lo que estaba haciendo:

([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}» 

seguida de la versión revisada:

([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

De acuerdo, $ debe ser el problema. – jessehouwing

Cuestiones relacionadas