你可能想看看在re.match
的文件,因为它指出:
如果在字符串开头的零个或多个字符匹配正则表达式
注意:开始。
你可以改为使用`re.findall'来标记每个键到它们的int。 IE:
>>> s = ':{\"page_type\":16,\"actor_id\":100010050258799,\"story_id\":166366493708358,\"a'
>>> re.findall('"([^"]+)":(\d+)',s)
[('page_type', '16'), ('actor_id', '100010050258799'), ('story_id', '166366493708358')]
>>>
>>> d = dict(re.findall('"([^"]+)":(\d+)',s))
>>> d
{'actor_id': '100010050258799', 'page_type': '16', 'story_id': '166366493708358'}
注意模式:
"([^"]+)":(\d+)
" #matches `"` char
([^"]+) #matches anything EXCEPT `"` char, and captures in a group
": #matches `":` chars
(\d+) #matches 1 or more digits and captures in a group