简体   繁体   English

删除两个定界符之间的文本的大多数pythonic方法

[英]Most pythonic way to delete text between two delimiters

I'm trying to remove wiki formatting from some text so it can be parsed. 我正在尝试从某些文本中删除Wiki格式,以便可以对其进行解析。

What is the most pythonic way to remove two delimiters ('[[' and ']]') all the text between them? 删除两个定界符('[['和']]')之间所有文本的最有效方法是什么? The given string will contain multiple occurrences of delimiter pairs. 给定的字符串包含多次出现的定界符对。

Regular expressions are a good match for your problem. 正则表达式非常适合您的问题。

>>> import re
>>> input_str = 'foo [[bar]] baz [[etc.]]'

If you are wanting to remove the whole [[...]] , which is I think what you are asking about, 如果您想删除整个[[...]] ,我想您是在问什么,

>>> re.sub(r'\[\[.*?\]\]', '', input_str)
'foo  baz '

If you are wanting to leave the contents of the [[...]] in, 如果您想保留[[...]]的内容,

>>> re.sub(r'\[\[(.*?)\]\]', r'\1', input_str)
'foo bar baz etc.'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM