我想用wiki标记替换字符串中的一些子字符串。E、 我有一根绳子
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
我想用
^{pr2}$我尝试了一些正则表达式来提取{{}}中的内容。但我总是一无所获。在
ADD:问题是我只对[[]]中的内容感兴趣,它本身就在{{}}中。我在字符串的另一部分还有一些[[]]的出现。在
那么,我怎么能用回复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)
匹配而不是
replacing
它.*?
匹配懒洋洋的它将在第一次出现]]
时停止.*
匹配贪婪。所以它将在最后一次]]
发生时停止(?=[^{]*}})
是一个lookahead
,它意味着匹配[[ ]]
中的内容,前提是它后跟0到多个字符,除了{
到{这样做是因为您想要匹配
[[``]]
,如果它在{{
}}
内。。在所以
]]
之后的字符是{
到}}
之外的任何字符。。在所以这样可以避免类似的情况
^{pr2}$尝试使用非贪心的regexp,类似于: r“{\{.*?\}\}““
您可以尝试以下操作:
更新 通过附加的约束(只有{{}}内的文本匹配),您可以通过两个步骤实现目标:
您可以按如下方式执行(我使用的源字符串包含不匹配的方形braquets中的文本):
^{pr2}$相关问题 更多 >
编程相关推荐