2009-07-13 50 views
0

我前段時間做了一個REGEX模式,我不記得它的意思。對我來說,這是一個只寫語言:)解釋這個特定的REGEX

這裏是正則表達式:

"(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,10})$" 

我需要知道,用簡單的英語,這是什麼意思。

+2

如果你不能讀正則表達式,你不應該寫它們。 – Tomalak 2009-07-13 10:24:41

+3

如果你從來沒有學過正則表達式,並且你突然維護包含它們的代碼,那麼這是一個合理的問題。 – Mark 2009-07-13 12:37:13

回答

6
(?!^[0-9]*$) 

不長僅匹配數字,

(?!^[a-zA-Z]*$) 

不匹配字母,

^([a-zA-Z0-9]{8,10})$ 

匹配的字母和數字8到10個字符。

2

RegexBuddy說了這樣的(!?!):

(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,10})$ 

Options:^and $ match at line breaks 

Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?!^[0-9]*$)» 
    Assert position at the beginning of a line (at beginning of the string or after a line break character) «^» 
    Match a single character in the range between 「0」 and 「9」 «[0-9]*» 
     Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*» 
    Assert position at the end of a line (at the end of the string or before a line break character) «$» 
Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?!^[a-zA-Z]*$)» 
    Assert position at the beginning of a line (at beginning of the string or after a line break character) «^» 
    Match a single character present in the list below «[a-zA-Z]*» 
     Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*» 
     A character in the range between 「a」 and 「z」 «a-z» 
     A character in the range between 「A」 and 「Z」 «A-Z» 
    Assert position at the end of a line (at the end of the string or before a line break character) «$» 
Assert position at the beginning of a line (at beginning of the string or after a line break character) «^» 
Match the regular expression below and capture its match into backreference number 1 «([a-zA-Z0-9]{8,10})» 
    Match a single character present in the list below «[a-zA-Z0-9]{8,10}» 
     Between 8 and 10 times, as many times as possible, giving back as needed (greedy) «{8,10}» 
     A character in the range between 「a」 and 「z」 «a-z» 
     A character in the range between 「A」 and 「Z」 «A-Z» 
     A character in the range between 「0」 and 「9」 «0-9» 
Assert position at the end of a line (at the end of the string or before a line break character) «$» 
4

Perl(和Python相應)說,到(?!...)部分:

零寬度負預測先行斷言。例如/foo(?!bar)/匹配任何不是「bar」後面的「foo」。但請注意,前視和後視不一樣。你不能用它來看後面。

這意味着,

(?!^[0-9]*$) 

意味着:不匹配,如果字符串包含數字。^:開始行/字符串,$:行結束/字符串)另一個相應的。

您的正則表達式匹配任何字符串,其中包含兩個數字和字母,但不只是其中之一。

乾杯,

更新:對於你的未來正則表達式的剪裁,看看在(?#...)模式。它允許你在你的正則表達式中嵌入註釋。還有一個修飾語,re.X,但我不太喜歡這個。這是你的選擇。