我在为这个任务找到正确的正则表达式时遇到了一些问题,请原谅我的初学者技能。我想做的只是从“可用”的行中获取 id 值:true 而不是“可用”:false。我能够通过 re.findall('"id":(\d{13})', line, re.DOTALL) 获取所有行的 ID(13 正好匹配 13 位数字,因为代码中还有其他 id 少于 13 位,我不需要)。
{"id":1351572979731,"parent_pid":21741,"available":false,"lou":"678","feature":true,"pub":true,"require":null,"option4":""},
{"id":1351572329731,"parent_pid":21741,"available":false,"lou":"678","feature":true,"pub":true,"require":null,"option4":""},
{"id":1351572943231,"parent_pid":21741,"available":true,"lou":"678","feature":true,"pub":true,"require":null,"option4":""},
{"id":1651572973431,"parent_pid":21741,"available":true,"lou":"678","feature":true,"pub":true,"require":null,"option4":""},
因此最终结果需要是 ['1651572973431','1351572943231']
感谢大大的帮助谢谢
请您参考如下方法:
这可能不是一个好的答案——这取决于你拥有什么。 看起来您有一个字符串列表,并且您想要其中一些字符串的 id。如果是这样的话,如果您解析 JSON 而不是编写拜占庭式正则表达式,那么它会多更清晰且更易于阅读。例如:
import json
# lines is a list of strings:
lines = ['{"id":1351572979731,"parent_pid":21741,"available":false,"lou":"678","feature":true,"pub":true,"require":null,"option4":""}',
'{"id":1351572329731,"parent_pid":21741,"available":false,"lou":"678","feature":true,"pub":true,"require":null,"option4":""}',
'{"id":1351572943231,"parent_pid":21741,"available":true,"lou":"678","feature":true,"pub":true,"require":null,"option4":""}',
'{"id":1651572973431,"parent_pid":21741,"available":true,"lou":"678","feature":true,"pub":true,"require":null,"option4":""}',
]
# parse it and you can use regular python to get what you want:
[line['id'] for line in map(json.loads, lines) if line['available']]
结果
[1351572943231, 1651572973431]
如果你发的代码是一长串,你可以把它包在[]中,然后把它解析成一个数组,结果是一样的:
import json
line = r'{"id":1351572979731,"parent_pid":21741,"available":false,"lou":"678","feature":true,"pub":true,"require":null,"option4":""}, {"id":1351572329731,"parent_pid":21741,"available":false,"lou":"678","feature":true,"pub":true,"require":null,"option4":""}, {"id":1351572943231,"parent_pid":21741,"available":true,"lou":"678","feature":true,"pub":true,"require":null,"option4":""},{"id":1651572973431,"parent_pid":21741,"available":true,"lou":"678","feature":true,"pub":true,"require":null,"option4":""}'
lines = json.loads('[' + line + ']')
[line['id'] for line in lines if line['available']]
