python_re_module

1: select fdsa||fasdfers||wafdsf cfdm

select concat(fdsa,fasdfers,wafdsf) cfdm

 

2: to_date(#trxDate#,'yyyyMMdd hh24:mi:ss')

str_to_date(#trxDate#,'%y%m%d %h:%i:%s')

 

cankao:

text="to_date(#trxDate#,'yyyyMMdd hh24:mi:ss')"
p=re.compile(r"(to_date)(.+?')([a-zA-Z])(\3+)([^a-zA-Z]?)([a-zA-Z])(\6+)([^a-zA-Z]?)([a-zA-Z])(\9+)(.+?):(.+?):(.+)")
p.sub(r"str_\1\2%\3\5%\6\8%\9 %H:%i%s'",text)

 

 答案

import re
text="select fdsa||fasdfers||wafdsf cfdm"
new_text=" ".join(["concat("+",".join(t.split("||"))+")" if "||" in t else t for t in text.split(" ")])


text="fdsfds to_date(#trxDate#,'yyyyMMdd hh24:mi:ss') fds to_date(#trxDate#,'yyyyMMdd hh24:mi:ss') to_date(#trxDate#,'yyyyMMdd hh24:mi:ss') ftgnfghtr"
p=re.compile(r"to_date\(.+?\)")
w = [(m.start(),m.end()) for m in re.finditer(p, text)]
res=""
pre=0
end=len(text)
for ww in w:
    res=res+text[pre:ww[0]]
    a,_=text[ww[0]:ww[1]].split(",")
    a="str_"+a+","+"'%y%m%d %h:%i:%s')"
    res+=a
    pre=ww[1]
res=res+text[pre:end]
print(res)
posted @ 2020-01-15 19:17  applejuice  阅读(137)  评论(0)    收藏  举报