0

我想用 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)
4

3 回答 3

0

尝试使用非贪婪的正则表达式,例如: r"\{\{.*?\}\}"

于 2012-12-05T14:43:26.953 回答
0

匹配它而不是replacing

\[\[.*?\]\](?=[^{]*\}\})

.*?懒惰地匹配。所以它会在第一次]]发生时停止

.*贪婪地匹配。所以它会在最后一次]]发生时停止


(?=[^{]*}}) is a lookahead which 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

[[xyz]]<-this would not match since { after it
{{
[[xyz]]<-this would match since it is not followed by { and it reaches }}
[[xyz]]<-this would match since it is not followed by { and it reaches }}
}}
于 2012-12-05T14:46:02.003 回答
0

You can try the following:

In [10]: p = "\[\[.*?\]\]"
In [11]: s1 = '\n'.join(re.findall(p, s))

Update With your additional constrain (only text inside {{}} matches) you can achieve your goal in two steps:

  • select the text inside braces
  • then select the text inside square braquets

You can do it as follow (I use a source string containing text in square braquets that doesn't match):

In [157]: print s
some [[other string before]]
Methods("")
{{columns-list|3|
* [[Cluster analysis]]
* [[Decision trees]]
* [[Factor analysis]]
}}
Application("domains")
{{columns-list|3|
* [[Analytics]]
* [[Bioinformatics]]
* [[Web mining]]
}}
some [[other string after]]

In [158]: p = "(?:\{\{)[\s\S]*?(?:\}\})"

In [159]: s1 = '\n'.join(re.findall(p, s))

In [160]: print s1
{{columns-list|3|
* [[Cluster analysis]]
* [[Decision trees]]
* [[Factor analysis]]
}}
{{columns-list|3|
* [[Analytics]]
* [[Bioinformatics]]
* [[Web mining]]
}}

In [161]: p1 = "\[\[.*\]\]"

In [162]: s2 = '\n'.join(re.findall(p1, s1))

In [163]: print s2
[[Cluster analysis]]
[[Decision trees]]
[[Factor analysis]]
[[Analytics]]
[[Bioinformatics]]
[[Web mining]]
于 2012-12-05T14:52:04.883 回答