I want to replace some substrings in a string with wiki markup. E.g. I have a string
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
I want to replace the original substring by
[[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]]
I’ve tried some regex expressions to extract stuff in {{ }} first. But I always got None.
ADD: The problem is that I’m only interested in the contents in [[]] which itself is in {{}}. I have some other occurrences of [[]] in other part of the string.
So, how could I do this by using re.sub? Thanks
ADD: current solution (ugly)
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)
Match it instead of
replacingit.*?matches lazily.so it would stop at the first time]]occurs.*matches greedily.so it would stop at the last time]]occurs(?=[^{]*}})is alookaheadwhich means match content within[[ ]]only if it is followed by 0 to many characters except{till}}..This is done because you want to match
[[``]]if it is within{{}}..So characters after
]]would be any character except{till}}..So this would avoid cases like this