> "100" 现在,例如我们在 a() 中有 a(-6ren">
gpt4 book ai didi

python - 使用 re.sub() 递归

转载 作者:太空宇宙 更新时间:2023-11-04 06:55:34 25 4
gpt4 key购买 nike

如果我们这样使用它:

re.sub("a\(\s*\d*\s*,\s*\d*\s*\)", "100", "a(440, 330)"

我们得到:

>> "100"

现在,例如我们在 a() 中有 a():

re.sub("a\(\s*\d*\s*,\s*\d*\s*\)", "100", "a(30, a(30,30))")

我们得到:

>> 'a(30, 100)'

但是我想要这样的东西:

1. a(30, a(30,30)) # replace the nested 'a' 
2. a(30, 100) # now work with the remainder
3. '100' # final string

对不起我的英语。

最佳答案

不断更换,直到没有东西可以更换为止:

while True:
output = re.sub("a\(\s*\d*\s*,\s*\d*\s*\)", "100", input)
if output == input:
break
input = output

关于python - 使用 re.sub() 递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23438365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com