gpt4 book ai didi

Python 正则表达式 : Matching bracket/parenthesis pairs

转载 作者:太空宇宙 更新时间:2023-11-03 13:21:33 25 4
gpt4 key购买 nike

我想捕获彼此相邻的方括号/圆括号对,并捕获其中的单词。在下面的文本中,我想捕捉 [oh](so)[bad](things)

[oh](so)funny
[all]the[bad](things)

如果我使用正则表达式 r'\[(.*?)\]\((.*?)\)' 它会捕获 [oh](so)[all]the[bad](things),这不是我想要的。

什么是解决这个问题的好正则表达式?

最佳答案

不要使用.*?

改为使用 [^\]]+[^\)]+

换句话说:

r'\[([^\]]+)\]\(([^\)]+)\)'

关于Python 正则表达式 : Matching bracket/parenthesis pairs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12132336/

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