Python regex - 用括号的内容替换括号内的文本 [英] Python regex - Replace bracketed text with contents of brackets

查看:51
本文介绍了Python regex - 用括号的内容替换括号内的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个 Python 函数,用大括号的内容替换用大括号括起来的文本实例,同时保留空的大括号对.例如:

foo {} bar {baz} 会变成 foo {} bar baz.

我创建的与此匹配的模式是 {[^{}]+},即一些不包含由一组包围的花括号(以防止重叠匹配)的文本花括号.

显而易见的解决方案是在我的模式中使用 re.sub,我发现我可以使用 \g<0> 引用匹配的文本:

<预><代码>>>>re.sub("{[^{}]+}", "A \g<0> B", "foo {} bar {baz}")'foo {} bar A {baz} B'

所以没问题.但是,我坚持如何从引用的文本中修剪括号.如果我尝试将范围应用于替换字符串:

<预><代码>>>>re.sub("{[^{}]+}", "\g<0>"[1:-1], "foo{}bar{baz}")'foo{}barg<0'

\g<0> 解析为匹配文本之前应用范围,并修剪前导 \ 和尾随 >code>,只留下g<0,没有特殊意义.

我也尝试定义一个函数来执行修剪:

def trimBraces(string):返回字符串[1:-1]

但是,不出所料,这并没有改变任何事情.

<预><代码>>>>re.sub("{[^{}]+}", trimBraces("\g<0>"), "foo{}bar{baz}")'foo{}barg<0'

我在这里错过了什么?非常感谢.

解决方案

您可以使用捕获组来替换匹配的一部分:

<预><代码>>>>re.sub(r"{([^{}]+)}", r"\1", "foo{}bar{baz}")'foo{}barbaz'>>>re.sub(r"{([^{}]+)}", r"\1", "foo {} bar {baz}")'foo {} bar baz'

I'm trying to write a Python function that replaces instances of text surrounded with curly braces with the contents of the braces, while leaving empty brace-pairs alone. For example:

foo {} bar {baz} would become foo {} bar baz.

The pattern that I've created to match this is {[^{}]+}, i.e. some text that doesn't contain curly braces (to prevent overlapping matches) surrounded by a set of curly braces.

The obvious solution is to use re.sub with my pattern, and I've found that I can reference the matched text with \g<0>:

>>> re.sub("{[^{}]+}", "A \g<0> B", "foo {} bar {baz}")
'foo {} bar A {baz} B'

So that's no problem. However, I'm stuck on how to trim the brackets from the referenced text. If I try applying a range to the replacement string:

>>> re.sub("{[^{}]+}", "\g<0>"[1:-1], "foo{}bar{baz}")
'foo{}barg<0'

The range is applied before the \g<0> is resolved to the matched text, and it trims the leading \ and trailing >, leaving just g<0, which has no special meaning.

I also tried defining a function to perform the trimming:

def trimBraces(string):
    return string[1:-1]

But, unsurprisingly, that didn't change anything.

>>> re.sub("{[^{}]+}", trimBraces("\g<0>"), "foo{}bar{baz}")
'foo{}barg<0'

What am I missing here? Many thanks in advance.

解决方案

You can use a capturing group to replace a part of the match:

>>> re.sub(r"{([^{}]+)}", r"\1", "foo{}bar{baz}")
'foo{}barbaz'
>>> re.sub(r"{([^{}]+)}", r"\1", "foo {} bar {baz}")
'foo {} bar baz'

这篇关于Python regex - 用括号的内容替换括号内的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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