33 - 用正则表达式判断字符串中是否包含日期

1. 请简要描述Python正则表达式中match函数的作用

import re

print(re.match('.*hello', 'ahello'))
<re.Match object; span=(0, 6), match='ahello'>

2. 如果日期的格式是4位年,2位月,2位日(如2012-01-02) ,如何使用正则表达式判断一个字符串中是否包含这样的日期

s = 'Today is 2020-02-21.'
m = re.match('.*\d{4}-\d{2}-\d{2}.*', s)

if m is not None:
    print(m.group())
Today is 2020-02-21.

34 - 寻找字符串中的手机号

发布了131 篇原创文章 · 获赞 134 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_29339467/article/details/104433449
33