2012-04-03 19 views
5

Ich versuche, eine .NET Regex zu verwenden, um das Eingabeformat einer Zeichenfolge zu überprüfen. Die Zeichenfolge kann dasEinfacher Ausdruck regulärer Ausdruck (Regex) (.net)

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

So 0AAA, 107ZF, 503GH, 0AAAA alle gültig sind. Der String, mit dem ich meine Regex Konstrukt ist wie folgt:

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

Doch dies lässt sich aber keine Strings, in dem der zweite Ausdruck einer von 03, 07 oder AA ist. Während des Debuggens entfernte ich den dritten Term aus der Zeichenkette, die zum Konstruieren der Regex verwendet wurde, und fand heraus, dass Eingabezeichenfolgen der Form 103, 507, 6AA validieren würden ...

Irgendwelche Ideen warum, wenn ich dann Setzen Sie den dritten Term wieder in die Regex, die Eingabezeichenfolgen wie 1AAGM stimmen nicht überein?

Dank Tom

+0

FYI, fand ich dieses Tool wirklich nützlich, um Regex zu testen http://gskinner.com/RegExr/ – michele

Antwort

9

Dies liegt daran, Ihr Ausdruck die Saiten mit 03, 07 und AA erfordert genau dort zu beenden ($ paßt das Ende der Eingabe). Entfernen Sie die $ aus diesen Unterausdrücken und verschieben Sie sie an das Ende des Ausdrucks.

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

+1, während meine Antwort erklärt, was er tat und eine Lösung bot. Diese Antwort stellt sicher, dass 107ZFD zum Beispiel nicht validiert wird, während meine zum 107ZF-Teil der Zeichenkette passt. Nicht sicher, was er will, aber das ist auch eine gute Antwort. – Matt

4

ich das glaube, ist, weil Sie das „$“ in der Regex verwenden, die in diesem Fall bedeutet, Position am Ende einer Zeile (am Ende des Strings oder vor einem Zeilenumbruch Zeichen zu behaupten). Entfernen Sie es und es sollte funktionieren. Von Regex Buddy, hier ist das, was du getan hast:

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

durch die überarbeitete Fassung Gefolgt:

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

Einverstanden, die $ sollten das Problem sein. – jessehouwing