python正则表达式,替换字符串中的模式

python正则表达式,替换字符串中的模式,python,regex,Python,Regex,我想用wiki标记替换字符串中的一些子字符串。我有一根绳子 some other string before ; Methods {{columns-list|3| * [[Anomaly detection|Anomaly/outlier/change detection]] * [[Association rule learning]] * [[Statistical classification|Classification]] * [[Cluster analysis]] * [[Dec

我想用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来实现这一点呢?谢谢

添加:当前解决方案

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)

尝试使用非贪婪的regexp,例如: r\{.*.\}

匹配它,而不是替换它

*?延迟匹配。因此它将在第一次出现时停止]]

.*贪婪地匹配。因此它将在最后一次]]发生时停止

?=[^{]*}}是一个前瞻,这意味着只有在[[]]中的内容后跟0到多个字符(除了{till})时,才匹配该内容

之所以这样做是因为如果[[``]]在{{}之内,您希望匹配它

所以]]之后的字符将是除{till}之外的任何字符

这样可以避免类似的情况

[[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 }}
}}

您可以尝试以下操作:

In [10]: p = "\[\[.*?\]\]"
In [11]: s1 = '\n'.join(re.findall(p, s))
更新 使用{{}}匹配项中的附加约束仅文本,您可以通过两个步骤实现目标:

选择大括号内的文本 然后选择方形braquets内的文本 您可以按如下操作:我使用一个源字符串,其中包含不匹配的方括号中的文本:

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]]

使用正则表达式适用于正则语言。我尝试了re.subr'{.*?}','',string,re.subr'\{.*?\}','',string和我在网上找到的其他更复杂的re表达式。我拿回了整根绳子。嗨,杰伯纳尔多。目前,我可以从文章中删除大多数wiki标记。在我的解析器中,我删除了{{}的每一次出现。我希望我能把内容保留在表格里。我在网上找到了一些wiki解析器,但这些解析器只将wiki标记转换为html.Hi Some1.Kill.the.DJ。我希望提取[[]]中的内容,而[[]]本身在{{}中。这是wiki标记中的一个表。嗨,Some1.Kill.The.DJ。谢谢你的表达。很酷!我不熟悉reg表达式。你介意解释一下什么是[^{]是指或向我提供一些指针吗?谢谢。如果我只想查找出现的情况,它就可以工作。我使用另一个函数regt获取并保留内容,然后再次将内容添加到字符串中。def regtmatchobj:store matchobj.group0某处return matches=re.subr'[[*.]]?=[^{]*\}',regt,还有更优雅的解决方案吗?嗨,Some1.Kill.The.DJ。知道了。非常感谢!嗨,申雪。它不起作用。re.matchr'{.*.}}',字符串返回None。对不起,\\Hi Vicent出现问题。谢谢尽管如此,我还是希望提取[[]]中的内容,而[[]]本身就在{{}中。我在字符串的其他部分出现了一些[[]]。
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]]