正则表达式电话和密码



嗨,我是 Python 的新手,并试图弄清楚如何检查电话号码的格式是否是正确的美国号码- 555-555-5555还要检查给定的密码是否具有小写字母、大写字母和特殊符号。这是我到目前为止所拥有的:

import re
inputText = raw_input("Please enter a string for testing:")
if re.match(r'(d{3}) D* (d{3}) D* (d{4}) D* (d*)', inputText):
    print ('Legitimate US phone number')
else:
    print ('Error is reported')
import re
password = raw_input("Please enter a string for testing:")
if re.match(r'((?=.*d)(?=.*[a-z])(?=.*[A-Z])(?=.*[#^*=%]).{6,15})', password):
    print ('Valid Password')    
else:
    print ('Error')

匹配您提供的表格的电话号码

>>> import re
>>> s = 'this is a test with 123-456-7890 a phone number'
>>> p = 'd{3}-d{3}-d{4}'
>>> re.findall(p, s)
['123-456-7890']

电话号码:

import re
inputText = raw_input("Please enter a string for testing:")
if re.match(r'd{3}-d{3}-d{4}', inputText):
    print ('Legitimate US phone number')
else:
    print ('Error is reported')

密码:

import re
password = raw_input("Please enter a string for testing:")
if re.match(r'^(?=.*[a-z])(?=.*[A-Z])(?=.*[#^*=%]).{6,15}$', password):
    print ('Valid Password')    
else:
    print ('Error')

我假设原则上所有字符都可以作为密码,并从您的代码中猜测:至少 6 个字符,最多 15 个字符。(为什么要限制密码的最大字符数,为什么要限制如此不合理的短字符数?

相关内容

最新更新