Python

Python基础(24) - 如何用正则表达式判断字符串中是否有

2020-03-04  本文已影响0人  xianling_he

match函数的用法

正则表达式中match函数的作用

import re
sFlag = re.match('hello','hello')
print(sFlag)
hexianling.png
sNewFlag = re.match('.*hello','abchello')
print(sFlag)
hexianling.png

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

s = 'Today is 2020-03-04'
m = re.match('.*\d{4}-\d{2}-\d{2}.*',s)
print(m)
hexianling.png
s = 'Today is 200-3-04'
m = re.match('.*\d{4}-\d{2}-\d{2}.*',s)

if m is not None:
    print(m.group())
else:
    print('None')
hexianling.png

加油 2020-3-4

上一篇 下一篇

猜你喜欢

热点阅读