remove digits NOT between brackets – regex(删除括号之外的数字-regex)

I am trying to write a regular expression in python to remove digits except those between brackets
here is an example : “[Verse 1: Bankroll Fresh] Dope boy, ‘[9]5 Air Max on Came from 1952..”

I would like : “[Verse 1: Bankroll Fresh] Dope boy, ‘[9] Air Max on Came from ..”

Solution:

Can you try this regex r'(?

st = "[Verse 1: Bankroll Fresh] Dope boy, '[9]5 Air Max on Came from 1952.."
st = re.sub(r'\[\D*\d+\D*\]|(?<!\[)\d+(?!\])', lambda x: x.group(0) if x.group(0).startswith('[') else '', st)

# [Verse 1: Bankroll Fresh] Dope boy, '[9] Air Max on Came from ..
print(st)

(?

————————

我正在尝试用python编写一个正则表达式,以除去括号之间的数字
这里有一个例子:“[1节:资金新鲜]毒品男孩,[9]5 Air Max on来自1952年……”

我想:“[1节:资金新鲜]毒品男孩,[9]Air Max on来自……”

解决方法:

你能试试这个正则表达式r’(?<!\[)\d+(?!\])吗?

st = "[Verse 1: Bankroll Fresh] Dope boy, '[9]5 Air Max on Came from 1952.."
st = re.sub(r'\[\D*\d+\D*\]|(?<!\[)\d+(?!\])', lambda x: x.group(0) if x.group(0).startswith('[') else '', st)

# [Verse 1: Bankroll Fresh] Dope boy, '[9] Air Max on Came from ..
print(st)

(?<!\[)和(?!\])分别是\d+前面不加[和后面不加]的匹配项。