【发布时间】:2011-02-05 01:38:04
【问题描述】:
我有一个简单的 Python 问题,我一直在思考。此代码 sn-p 有效。但是当我用 phoneNumber 替换“258 494-3929”时,出现以下错误:
# Compare phone number
phone_pattern = '^\d{3} ?\d{3}-\d{4}$'
# phoneNumber = str(input("Please enter a phone number: "))
if re.search(phone_pattern, "258 494-3929"):
print "Pattern matches"
else:
print "Pattern doesn't match!"
Pattern does not match
Please enter a phone number: 258 494-3929
Traceback (most recent call last):
File "pattern_match.py", line 16, in <module>
phoneNumber = str(input("Please enter a phone number: "))
File "<string>", line 1
258 494-3929
^
SyntaxError: invalid syntax
C:\Users\Developer\Documents\PythonDemo>
顺便说一句,我做了import re 并尝试在\n 的情况下使用rstrip
我还能错过什么?
【问题讨论】:
-
您是将其存储为字符串还是原始数据?
标签: python input command-line python-2.x