re例子,仅供参考

import re
匹配字符串
# pattern = re.compile(r'hello')

# match = pattern.search('ni hello cxy61!') #全字符匹配字符串 .search()
# search = pattern.match('ai hello') #只在开头匹配字符串 .match()
# if match:
# print(match.group())
# else:
# print('no match.')
#
# if search:
# print(search.group())
# else:
# print('no search')

替换
time = '2017-10-01'
pattern = re.compile(r'\D') #匹配非数字的字符
sub = pattern.sub('/',time) #替换非数字的字符
print(sub)
print(re.sub(r'\D','/',time)) #也可以这样写,re.sub(表达式,替换字符,内容)
#\D非数字的,'/'替换字符,time目标;把time里面非数字的都替换为/

猜你喜欢

转载自www.cnblogs.com/lc1013/p/10214603.html