[英]Regular expression in python that check if the string just contains letters, numbers and dot(.)
我正在尝试开发一个匹配的正则表达式,如果字符串仅在任意位置且无顺序地包含字母,数字,空格和点(。)。
像这样:
hello223 3423. ---> True
lalala.32 --->True
.hellohow1 ---> True
0you and me = ---> False (it contains =)
@newye ---> False (it contains @)
With, the name of the s0ng .---> False (it start with ,)
我正在尝试与此,但总是返回匹配项:
m = re.match(r'[a-zA-Z0-9,. ]+', word)
任何想法?
提出问题的其他方法是,字母,数字,点和空格是否有不同的字符?
提前致谢
您需要添加$
:
re.match(r'[a-zA-Z0-9,. ]+$', word)
re.search()
解决方案:
import re
def contains(s):
return not re.search(r'[^a-zA-Z0-9. ]', s)
print(contains('hello223 3423.')) # True
print(contains('0you and me = ')) # False
print(contains('.hellohow1')) # True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.