我正在使用以下代码:
email = "yash@"
string_check= re.compile('[@.]')
if(string_check.search(email) == None):
print('invalid')
else:
print('valid')
正确地评估为“有效”。但是,如果我要确保该字符串严格包含'@'和'。'。那我能达到什么目的呢?
我也尝试过下面的代码,但不适用于特殊字符:
import re
arr = ['@', '.']
# str = "hello people"
str = "yash"
if any(re.findall('|'.join(arr), str)):
print('Found a match')
else:
print('No match found')
尽管既没有“ @”也没有“”,但它的评估结果为“找到了匹配项”。
您可以使用all()
函数,而无需使用正则表达式:
arr = ['@', '.']
email = "yash@"
if all(ch in email for ch in arr):
print('valid')
else:
print('not valid')
印刷品:
not valid
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句