Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 955
Joan Triay Avatar asked Mar 12 '18 14:03

Joan Triay


2 Answers

You need to add $:

re.match(r'[a-zA-Z0-9,. ]+$', word)
like image 145
llllllllll Avatar answered Nov 07 '22 23:11

llllllllll


re.search() solution:

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
like image 25
RomanPerekhrest Avatar answered Nov 07 '22 23:11

RomanPerekhrest