替换所有括号内的特定元素 [英] Replace paticular element inside all parentheses

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

问题描述

所有特定的字母,比如A",需要在所有括号中替换.

All a specific letter, let's say 'A', needs to be replaced in all parentheses.

例如

A. (AbbAAbb) .A. (bbAbbAbA) .A. (bbbbAbbbb)

我想用 '' 替换括号中的所有 'A' 以这样结束:

I want to replace all 'A' in the parenthese with '' to end up like this:

A. (bbbb) .A. (bbbbb) .A. (bbbbbbbb)

有没有可能只用正则表达式来做到这一点?

Any possible to do this in only regular expression?

推荐答案

对于这种情况,有一个通用的解决方法 - 不应该存在不平衡/嵌套的括号(因为它们是).您查找紧跟在右括号之后但不匹配括号的 A:

There is a common workaround for such situations - no unbalanced / nested parentheses (as they are) should exist. You look for As that follow a closing parenthesis without matching a parenthesis:

A(?=[^)(]*\))

现场演示

Python 代码:

re.sub(r"A(?=[^)(]*\))", "", str)

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

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