我正在嘗試解析文件中的一些輸入文本,這些文本最初是從Twitter API抓取的。該文件是直接文本,在這種情況下我實際上並沒有抓住JSON。這是輸入文本的一個片段:如何讓我的RegEx捕獲冒號兩側的文本?
.....HootSuite</a>", "text": "For independent news reports on the crisis in #Japan,
see @DemocracyNow News Archive: http://ow.ly/4ht9Q
#nuclear #Fukushima #rdran #japon", "created_at": "Sat Mar 19.....
基本上我需要抓住這個:
"text": "For independent news reports "on" the crisis in #Japan, see @DemocracyNow
News Archive: http://ow.ly/4ht9Q #nuclear #Fukushima #rdran #japon"
這裏有兩個我試圖獲得工作,但我遇到一些麻煩他們:
re.findall('"text":[^_]*',line)
re.findall('"text":[^:}]+',line)
第一個將允許我抓住一切,直到後面的「創建」我想要的部分。第二個有點也適用,但是當文本中包含「:」時,它會一直持續到信息結束
任何人都有一些RegEx的經驗,可以指出我的方向是正確的嗎?
如果你從Twitter的API抓住它,我們可以假設你抓住了JSON?爲什麼不只是拉JSON值? http://pypi.python.org/pypi/simplejson/ – switz
這不是一個壞主意,好點。這裏是一個完整的數據行(儘可能多地粘貼) {「favorited」:false,「truncated」:false,「source」:「web」,「text」:「西漢姆推回馬刺隊在主場輸給熱刺將是重回歐冠賽場的嚴重挫折。「,」created_at「:」Sat Mar 19 14:19:19 +0000 2011「,」retweeted「:false,」class「 :「in_reply_to_status_id_str」:null,「id_str」:「49112718128594945」,「in_reply_to_screen_name」:null,「user」:{「follow_request_sent」:null,「profile_use_background_image」:true,「id」 :21619519,} – shenn