正则表达式:用条件替换子字符串

时间:2018-02-28 16:08:55

标签: python regex

我有文字,例如

[04.1_Filialy 680031, Khabarovsk Territory, Khabarovsk, ul. District, 6, building b, office 3.] and [04.1_OGRN660050463454]

欲望输出

<address> 680031, Khabarovsk Territory, Khabarovsk, ul. District, 6, building b, office 3.<\address> and [04.1_OGRN660050463454]

我需要在str中re.findall(r'\[[\d\.]+_(?:Filialy|MN)[^]]+],首先替换r'\[[\d\.]+_(?:Filialy|MN)'和r&#39;]&#39;到<address><\address>

我该怎么做?

1 个答案:

答案 0 :(得分:1)

[^]]+放入捕获论坛()并使用re.sub()

正则表达式\[[\d\.]+_(?:Filialy|MN)([^]]+)\]替换:<address>\1<\\address>

Python代码

re.sub(r'\[[\d\.]+_(?:Filialy|MN)([^]]+)\]', r'<address>\1<\\address>', str)

输出:

<address> 680031, Khabarovsk Territory, Khabarovsk, ul. District, 6, building b, office 3.<\address> and [04.1_OGRN660050463454]

Code demo