简体   繁体   English

python中的正则表达式,用于检查字符串是否仅包含字母,数字和点(。)

[英]Regular expression in python that check if the string just contains letters, numbers and dot(.)

I am trying to develop a regular expression for match if a string just contains letters, numbers, space and dot(.) anywhere and with no order. 我正在尝试开发一个匹配的正则表达式,如果字符串仅在任意位置且无顺序地包含字母,数字,空格和点(。)。

Like this: 像这样:

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 ,)

I am trying with this one but is always returning match: 我正在尝试与此,但总是返回匹配项:

m = re.match(r'[a-zA-Z0-9,. ]+', word)

Any idea? 任何想法?

Other way to formulate the question is are there any character diferent of letters, numbers, dot and space? 提出问题的其他方法是,字母,数字,点和空格是否有不同的字符?

Thanks in advance 提前致谢

您需要添加$

re.match(r'[a-zA-Z0-9,. ]+$', word)

re.search() solution: 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM