2011-09-20 91 views
-2

我也试过[a-zA-Z]{2,}-\d+但具有相同的结果不能得到预期的结果

def verify_commit_text(tags): 
    for line in tags: 
     if re.match('^NO-TIK',line): 
      return True 
     elif re.match('^NO-REVIEW', line): 
      return True 
     elif re.match('[a-zA-Z]-[0-9][0-9]', line): 
      return True 
     else: 
      return False 
if __name__ == '__main__': 
    commit_text_verified = verify_commit_text(os.popen('hg tip --template "{desc}"')); 
    #commit_text_verified = verify_commit_text(os.popen('hg log -r $1 --template "{desc}"')); 
    if (commit_text_verified): 
     sys.exit(0) 
    else: 
     print >> sys.stderr, ('[obey the rules!]') 
     sys.exit(1); 

如果我使用文本"JIRA-1234"正则表达式中:

elif re.match('[a-zA-Z]-[0-9][0-9]', line): 

似乎不工作我得到:

[obey the rules!] 

在标准输出。

回答

2

正则表达式的工作完全按照您指定它。它是寻找性格和位。你可能想是这样

re.match(r'[a-zA-Z]+-\d+\Z', line) 

此外,始终以“R”如上面的前缀正则表达式的字符串。或者它会咬你。

1

既然你想匹配一个或多个字母和数字,你需要使用+这样的:

r'[a-zA-Z]+-\d+' 

您还可以{}指定一定数量的字母(例如):

r'[a-zA-Z]{2,}-\d{4}' 

这里,{2,}指2个或更多,{4}表示正好4,{,3}装置0-3,并且{1,5}装置1-5的包容性。

相关问题