我想用 wiki 标记替换字符串中的一些子字符串。例如我有一个字符串
some other string before
; Methods
{{columns-list|3|
* [[Anomaly detection|Anomaly/outlier/change detection]]
* [[Association rule learning]]
* [[Statistical classification|Classification]]
* [[Cluster analysis]]
* [[Decision trees]]
* [[Factor analysis]]
* [[Neural Networks]]
* [[Regression analysis]]
* [[Structured data analysis (statistics)|Structured data analysis]]
* [[Sequence mining]]
* [[Text mining]]
}}
; Application domains
{{columns-list|3|
* [[Analytics]]
* [[Bioinformatics]]
* [[Business intelligence]]
* [[Data analysis]]
* [[Data warehouse]]
* [[Decision support system]]
* [[Drug Discovery]]
* [[Exploratory data analysis]]
* [[Predictive analytics]]
* [[Web mining]]
}}
some other string after
我想将原始子字符串替换为
[[Anomaly detection|Anomaly/outlier/change detection]]
[[Association rule learning]]
[[Statistical classification|Classification]]
[[Cluster analysis]]
[[Decision trees]]
[[Factor analysis]]
[[Neural Networks]]
[[Regression analysis]]
[[Structured data analysis (statistics)|Structured data analysis]]
[[Sequence mining]]
[[Text mining]]
[[Analytics]]
[[Bioinformatics]]
[[Business intelligence]]
[[Data analysis]]
[[Data warehouse]]
[[Decision support system]]
[[Drug Discovery]]
[[Exploratory data analysis]]
[[Predictive analytics]]
[[Web mining]]
我首先尝试了一些正则表达式来提取 {{ }} 中的内容。但我总是没有。
ADD:问题是我只对 [[]] 中的内容感兴趣,它本身就在 {{}} 中。我在字符串的其他部分还出现了其他一些 [[]] 。
那么,我怎么能通过使用 re.sub 来做到这一点?谢谢
ADD:当前解决方案(丑陋)
def regt(matchobj):
#store matchobj.group(0) somewhere else, later on add them to the string
#Next, another function will remove all {{}} alway
return ''
matches = re.sub(r'\[\[.*?\]\](?=[^{]*\}\})', regt,wiki_string2)