我正在使用 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/