字符串python的re/regex模式

use*_*490 3 python regex

我试图找出一个正则表达式模式来查找字符串中的所有出现:

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: []
Run Code Online (Sandbox Code Playgroud)

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

iCo*_*dez 8

方括号是Regex语法中的特殊字符.所以,你需要逃避它们:

>>> 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']
>>>
Run Code Online (Sandbox Code Playgroud)