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

标签 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: []

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

最佳答案

方括号是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/

相关文章:

Python Spark 数据帧 : Better way to export groups to text file

python - 在 tkinter 中获取子元素的宽度

python - TensorFlow:在不同输出形状的数据集之间交替

正则表达式,我只是不明白

python - 提取大字符串的部分

python - Python-实现自定义异常

python paramiko 关闭连接时出现问题

python - 如何将表达式字符串拆分为 Python 中的列表?

javascript - 用于匹配包含 ID 的工作表名称并使其可见的 Apps 脚本

javascript - 我需要创建一个四位数字的输入(所有数字都应该不同,第一个数字应该是 1)