gpt4 book ai didi

python - 需要正则表达式 :(

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

我正在使用 Python,我需要能够获取以下形式的字符串

abc | pqr | [1,2,3,4,5]

并从中获取实际的整数数组 [1,2,3,4,5]。有什么建议吗?

最佳答案

假设abc | pqr | 部分是文字字符,您想要:

import re
import ast

m = re.match(r"abc \| pqr \| (\[[-0-9,]*\])", inString)
if m is not None:
theList = ast.literal_eval(m.group(1))

如果您想跳过前导的不匹配字符,请使用search 而不是match

关于python - 需要正则表达式 :(,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4103544/

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