2009-07-13 47 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,但我不太喜欢这个。这是你的选择。