1. 程式人生 > >python替換指定字元

python替換指定字元

有如下一段文字,要求將中括號[]後的第一個分號;替換為換行符

[Souma, Kousaku;Kanda, Fumie;Masuko, Takayoshi] Tokyo Univ Agr, Fac Bioind, Abashiri, Hokkaido 0992493, Japan;[Wang, Peng] Jgfdsa Univ, Coll Ansfdm Sci & Vet Med, Chanfdsn 130023, Jfdsn, Peoples R China;[Igarashi, Hiroaki] Hokuren Federat Agr Cooperat Assoc, Obihiro Branch Off, Obihiro, Hokkaido, Japan

思路,用正則表示式定位分號位置,用subn替換

a="[Souma, Kousaku;Kanda, Fumie;Masuko, Takayoshi] Tokyo Univ Agr, Fac Bioind, Abashiri, Hokkaido 0992493, Japan;[Wang, Peng] Jgfdsa Univ, Coll Ansfdm Sci & Vet Med, Chanfdsn 130023, Jfdsn, Peoples R China;[Igarashi, Hiroaki] Hokuren Federat Agr Cooperat Assoc, Obihiro Branch Off, Obihiro, Hokkaido, Japan"
pattern=re.compile(r"(\[.*?\].*?)(;)(?=\[)")
pattern.subn(r'\1\n',a)[0]
輸出結果:

'[Souma, Kousaku;Kanda, Fumie;Masuko, Takayoshi] Tokyo Univ Agr, Fac Bioind, Abashiri, Hokkaido 0992493, Japan\n[Wang, Peng] Jgfdsa Univ, Coll Ansfdm Sci & Vet Med, Chanfdsn 130023, Jfdsn, Peoples R China\n[Igarashi, Hiroaki] Hokuren Federat Agr Cooperat Assoc, Obihiro Branch Off, Obihiro, Hokkaido, Japan'
注意:

1、在.*後加?禁用貪婪模式,否則只能搜尋到最後一個分號

2、subn的意思是將匹配到的串用正則表示式r'\1\n'替換,其中\1即pattern中第一個分組,第二個分組是分號用\n替代

python版本2.7.3