时间:2021-07-01 10:21:17 帮助过:50人阅读
因为没有很合适的module,所以得用regrex替换。数字很容易,字符串需要考虑
代码如下import re
sql = r"select * from a where id=‘aaaaa haha wocao‘ and id1= ‘fff‘ and xx=1 and a3= 4 and a4=3434343 and a5a>99" sql.split("‘") #[‘select * from a where id=‘, ‘aaaaa haha wocao‘, ‘ and id1= ‘, ‘fff‘, ‘ and xx=1 and a3= 4 and a4=3434343 and a5a>99‘] sarr = sql.split("‘") sarr #[‘select * from a where id=‘, ‘aaaaa haha wocao‘, ‘ and id1= ‘, ‘fff‘, ‘ and xx=1 and a3= 4 and a4=3434343 and a5a>99‘] sarr[::2] #[‘select * from a where id=‘, ‘ and id1= ‘, ‘ and xx=1 and a3= 4 and a4=3434343 and a5a>99‘] sarr[1::2] #[‘aaaaa haha wocao‘, ‘fff‘] sarr[1::2]= [‘?‘ for x in sarr[1::2]] sarr #[‘select * from a where id=‘, ‘?‘, ‘ and id1= ‘, ‘?‘, ‘ and xx=1 and a3= 4 and a4=3434343 and a5a>99‘] "‘".join(sarr) #"select * from a where id=‘?‘ and id1= ‘?‘ and xx=1 and a3= 4 and a4=3434343 and a5a>99" sql #"select * from a where id=‘aaaaa haha wocao‘ and id1= ‘fff‘ and xx=1 and a3= 4 and a4=3434343 and a5a>99"
aa = "‘".join(sarr)
re.sub(r"\b\d+\b","?",aa)
#"select * from a where id=‘?‘ and id1= ‘?‘ and xx=? and a3= ? and a4=? and a5a>?"
python正则表达式抽取mysql慢查询sql本身,de-parameterize,将参数值改为?
标签:mysq targe class pre sql慢查询 独立 extract slow 一个