删除两个分隔符之间文本的最pythonic方法 [英] Most pythonic way to delete text between two delimiters

查看:33
本文介绍了删除两个分隔符之间文本的最pythonic方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从某些文本中删除 wiki 格式,以便对其进行解析.

删除两个分隔符('[[' 和 ']]')之间所有文本的最pythonic 方法是什么?给定的字符串包含多次出现的分隔符对.

解决方案

正则表达式非常适合解决您的问题.

<预><代码>>>>进口重新>>>input_str = 'foo [[bar]] baz [[etc.]]'

如果您想删除整个 [[...]],我想这就是您要问的,

<预><代码>>>>re.sub(r'\[\[.*?\]\]', '', input_str)'富巴兹'

如果您想保留 [[...]] 的内容,

<预><代码>>>>re.sub(r'\[\[(.*?)\]\]', r'\1', input_str)'foo bar baz 等'

I'm trying to remove wiki formatting from some text so it can be parsed.

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.'

这篇关于删除两个分隔符之间文本的最pythonic方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆