Python正則運算式re.sub()函式的問題:
import fileinput, re
filed_pat = re.compile(r'\[(.+?)\]')
scope = {}
lines = []
for line in fileinput.input('1.txt'):
lines.append(line)
text = ''.join(lines)
def replacement(match):
code = match.group(1)
try:
return str(eval(code, scope))
except SyntaxError:
exec(code, scope)
return ''
print(filed_pat.sub(replacement, text))
在re.sub()函式中,以replacement函式作為sub函式第二個引數repl時,sub函式是怎么向replacement函式傳遞引數的?傳遞的是sub函式查找到與filed_pat匹配時回傳的那個MatchObject物件嗎?
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/228561.html
