2012-04-03 37 views
5

Tôi đang cố sử dụng .NET Regex để xác thực định dạng đầu vào của chuỗi. Chuỗi có thể có định dạngVấn đề biểu thức chính quy đơn giản (Regex) (.net)

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

Vì vậy, 0AAA, 107ZF, 503GH, 0AAAA đều hợp lệ. Chuỗi mà tôi xây dựng Regex của tôi như sau:

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

Tuy nhiên, điều này không xác thực các chuỗi có thời hạn thứ hai là một trong 03, 07 hoặc AA. Trong khi gỡ lỗi, tôi đã xóa thuật ngữ thứ ba khỏi chuỗi được sử dụng để xây dựng regex và thấy rằng các chuỗi đầu vào của biểu mẫu 103, 507, 6AA WOULD xác thực .......

Mọi ý tưởng tại sao, khi tôi đặt thuật ngữ thứ ba trở lại vào Regex, các chuỗi đầu vào như 1AAGM không khớp?

Cảm ơn Tom

+0

FYI, tôi thấy công cụ này thực sự hữu ích để kiểm tra regex http://gskinner.com/RegExr/ – michele

Trả lời

9

Điều này là do biểu hiện của bạn yêu cầu các chuỗi với 03, 07AA để chấm dứt ngay tại đó ($ phù hợp với kết thúc đầu vào). Xóa $ khỏi các biểu thức con này và di chuyển đến cuối biểu thức.

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

+1, trong khi câu trả lời của tôi giải thích những gì anh ta đang làm và đưa ra một giải pháp. Câu trả lời này đảm bảo rằng 107ZFD, ví dụ sẽ không xác nhận, trong khi tôi sẽ phù hợp với phần 107ZF của chuỗi. Không chắc chắn đó là những gì anh ta muốn, nhưng đây cũng là một câu trả lời hay. – Matt

4

Tôi tin rằng điều này là do bạn đang sử dụng "$" trong regex, có nghĩa là trong trường hợp này để khẳng định vị trí ở cuối dòng (ở cuối chuỗi hoặc trước ký tự ngắt dòng)). Loại bỏ nó và nó sẽ hoạt động. Từ Regex Buddy, đây là những gì bạn đã làm:

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

Tiếp theo là phiên bản sửa đổi:

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

Đồng ý, $ 's nên là vấn đề. – jessehouwing

Các vấn đề liên quan