1. match函数的作用
import re print(re.match('hello', 'hello')) print(re.match('.*hello', 'hello')) print(re.match('hello', 'ahello'))
<re.Match object; span=(0, 5), match='hello'>
<re.Match object; span=(0, 5), match='hello'>
None
2. 如果日期的格式是4伟年,2位月,2位日(如2012-0102),如何使用正则表达式判断一个字符串中是否包含这样的日期
import re s = 'Today is 2013-12-12.' m = re.match('.*\d{4}-\d{2}-\d{2}.*', s) print(m) if m is not None: print(m.group())
<re.Match object; span=(0, 20), match='Today is 2013-12-12.'>
Today is 2013-12-12.