【问题标题】:python find all strings starting and ending with certain characters [duplicate]python查找所有以特定字符开头和结尾的字符串[重复]
【发布时间】:2023-01-10 09:29:42
【问题描述】:

我如何用 python 提取所有以 'a[' 开头并以 ']' 结尾的字符串?

例如

str= "a[0]*x**3+a[13]"

result : a[0], a[13]

谢谢

【问题讨论】:

  • 使用正则表达式将帮助您解决这个问题

标签: python string findall


【解决方案1】:

我们可以在这里使用re.findall

inp = "a[0]*x**3+a[13]"
matches = re.findall(r'a[.*?]', inp)
print(matches)  # ['a[0]', 'a[13]']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-23
    • 1970-01-01
    • 2015-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-22
    相关资源
    最近更新 更多