You have to use search
instead of match
Here is what doc says
Python offers two different primitive operations based on regular
expressions: re.match() checks for a match only at the beginning of
the string, while re.search() checks for a match anywhere in the
string (this is what Perl does by default).
The strings provided had invalid \x
escape .To use them as row string you may use r"string" .The s1
and s2
variables be written as
s1=r"historyData\xx\n3_1010366372_2017-01-25_1126807"
s2=r"historyData\xx\2017-01-23\n3_1010366372_2017-01-25_1126807"
You may re-write the function as follows.
import re
def containsDate(s):
date_reg_exp = re.compile(r'(\d{4}-\d{2}-\d{2})')
mat = re.search(date_reg_exp,s)
return mat is not None
Now the functions may be used as follows
s1=r"historyData\xx\n3_1010366372_2017-01-25_1126807"
s2=r"historyData\xx\2017-01-23\n3_1010366372_2017-01-25_1126807"
if containsDate(s1):
print "match"
else:
print "no match"