标签:pre count pwd use imp dal span print 正则
# 正则 正则式针对字符串的操作
import re
s = ‘{"mobilephone": "${borrow_user}", "pwd": "${borrow_pwd}"}‘
d = {"mobilephone": "18511295864", "pwd": "123456"}
p = ‘\$\{(.*?)}‘
# 查找一个
m = re.search(p, s)
print(m) # <re.Match object; span=(17, 31), match=‘${borrow_user}‘>
g = m[1]
print(g) # borrow_user
#查找所有
m_all = re.findall(p, s)
print(m_all) # 返回列表 [‘borrow_user‘, ‘borrow_pwd‘]
# 查找并替换全部, 【count=1,就替换一次】
rep = ‘18511295864‘
m = re.sub(p, rep, s, count=1)
print(m) # {"mobilephone": "18511295864", "pwd": "${borrow_pwd}"}
标签:pre count pwd use imp dal span print 正则
原文地址:https://www.cnblogs.com/sophia-985935365/p/12642709.html