2012-04-03 20 views
5

Próbuję użyć regeksu .NET do sprawdzenia formatu wejściowego ciągu znaków. Łańcuch może być formatuProste wyrażenie regularne (Regex) (.net)

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

Więc 0AAA, 107ZF, 503GH, 0AAAA są poprawne. Łańcuch z którą skonstruować moje Regex jest następujący:

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

Jednak to nie sprawdza ciągi, w których drugi składnik jest jednym z 03, 07 lub AA. Podczas debugowania usunąłem trzeci termin z ciągu używanego do skonstruowania regex i odkryłem, że ciągi wejściowe w postaci 103, 507, 6AA powinny potwierdzać .......

Wszelkie pomysły, dlaczego, kiedy ja umieścić trzeci termin z powrotem w Regex, ciągi wejściowe, takie jak 1AAGM nie pasują?

Dzięki Tom

+0

FYI, znalazłem to narzędzie naprawdę przydatne do przetestowania regex http://gskinner.com/RegExr/ – michele

Odpowiedz

9

To dlatego, że ekspresja wymaga sznurki z 03, 07 i AA kończyć tam ($ dopasowuje koniec wejściu). Usuń $ z tych wyrażeń podrzędnych i przenieś do końca wyrażenia.

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

+1, podczas gdy moja odpowiedź wyjaśnia, co robił i zaproponowała rozwiązanie. Ta odpowiedź gwarantuje, że 107ZFD na przykład nie zostanie zatwierdzone, a moje będą pasowały do ​​części 107ZF ciągu. Nie jestem pewien, czego chce, ale jest to również dobra odpowiedź. – Matt

4

wierzę, to dlatego, że używasz „$” w regex, co oznacza w tym przypadku twierdzić pozycję na końcu linii (na końcu łańcucha lub przed znakiem podziału wiersza). Usuń go i powinien działać. Od Regex Buddy, o to co robisz:

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

Obserwowani przez zmienionej wersji:

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

Zgoda, dolary powinny być problemem. – jessehouwing

Powiązane problemy