您好我需要检查字符串是否适合python中的模式。有问题的模式是英国标准汽车注册。模式是
例如
AB56 TYR
如何让程序检查用户输入的注册是否符合此模式?
答案 0 :(得分:2)
import re
if re.match(r'^[A-Z][A-Z]\d\d [A-Z][A-Z][A-Z]$', text):
# valid
答案 1 :(得分:0)
这是我想出的
import re
text = 'AB56 TYR'
text2='Adfsdf56 TYR'
m = re.search('^([A-Z]{3}\s?(\d{3}|\d{2}|d{1})\s?[A-Z])|([A-Z]\s?(\d{3}|\d{2}|\d{1})\s?[A-Z]{3})|(([A-HK-PRSVWY][A-HJ-PR-Y])\s?([0][2-9]|[1-9][0-9])\s?[A-HJ-PR-Z]{3})', text)
n = re.search('^([A-Z]{3}\s?(\d{3}|\d{2}|d{1})\s?[A-Z])|([A-Z]\s?(\d{3}|\d{2}|\d{1})\s?[A-Z]{3})|(([A-HK-PRSVWY][A-HJ-PR-Y])\s?([0][2-9]|[1-9][0-9])\s?[A-HJ-PR-Z]{3})', text2)
if m:
print "NUMBER IS GOOD"
else:
print "BAD NUMBER"
if n:
print "NUMBER IS GOOD"
else:
print "BAD NUMBER"