s = '"info":"infotext","day":"today","12":"here","info":"infotext2","info":"infotext3"'
import json
def pairs_hook(pairs):
return [val for key, val in pairs if key == 'info']
p = json.loads('{' + s + '}', object_pairs_hook=pairs_hook)
print p # [u'infotext', u'infotext2', u'infotext3']
object_pairs_hook is an optional function that will be called with the result of any object literal decoded with an ordered list of pairs. The return value of object_pairs_hook will be used instead of the dict.
使用此正则表达式
(?<="info":")(.+?)(?=")
只要您的
infotext
不包含(转义)引号,您可以尝试如下操作:我们只需匹配
"info":"
和尽可能多的非"
字符(这些字符被捕获并返回)。在使用json,Luke
来自the docs:
为了完整起见,下面是一个正则表达式,它执行相同的操作:
^{pr2}$它还处理
:
左右的空格和字符串中的转义引号,例如相关问题 更多 >
编程相关推荐