我有文字,例如
[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>
。
我该怎么做?
答案 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]