【发布时间】:2017-05-02 10:09:08
【问题描述】:
我想使用 try/except 语句来检查字符串是否仅由字母组成。 以下有什么问题
class LetterError(Exception):
pass
name = ""
while name=="":
try:
x = re.match(r'[a-zA-Z]',(input("Please enter a name: ")))
raise LetterError
except LetterError :
print("Insert letters only")
【问题讨论】:
-
那么你需要至少使用
r'[a-zA-Z]+$'和re.match。然后只提出错误if not x。 -
您只使用了
[A-Za-z]中的一个字母。添加+像这样[A-Za-z]+。这意味着很多字母。 -
@monika 我更新了正则表达式。看看
标签: python regex exception try-catch