你可能想看看在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