gpt4 book ai didi

python - 字符串python的重新/正则表达式模式

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

我正在尝试找出一个正则表达式模式来查找字符串中的所有匹配项:

string = "List[5] List[6], List[10], List[100:] List[-2:] List[-2]"
re.findall("List[(.*?)]" , string)
# Expected output ['5', '6', '10', '100:', '-2:', '-2']
# Output: []

获取索引之间的数字的良好正则表达式模式是什么?

最佳答案

方括号是Regex's syntax中的特殊字符.所以,你需要逃避他们:

>>> import re
>>> string = "List[5] List[6], List[10], List[100:] List[-2:] List[-2]"
>>> re.findall("List\[(.*?)\]", string)
['5', '6', '10', '100:', '-2:', '-2']
>>>

关于python - 字符串python的重新/正则表达式模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20598241/

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