2016-07-11 85 views
-1

我想提取對象(如Regions和Countries)的idtype,其中available=true。我用正則表達式{"name":".+?","id":"(.+?)","type":"(.+?)","available":true避免完全匹配的正則表達式

我得到一個從名稱開始的完整行... {other in the middle} ... available = true其中包括其他ID /名稱,即使在available=false也是如此。下面

{ 
"error":null, 
"data":{ 
    "airports":[], 
    "pocs":[], 
    "regions":[ 
    { 
     "name":"Central America", 
     "id":"L04305", 
     "type":"CruiseArea", 
     "available":false, 
     "countries":"Mexico", 
     "group":null 
    }, 
    { 
     "name":"Caribbean", 
     "id":"L04304", 
     "type":"CruiseArea", 
     "available":false, 
     "countries":"St Maarten,Barbados,British Virgin Islands,St Kitts and Nevis,St Vincent and the Grenadines,Antigua", 
     "group":null 
    }, 
    { 
     "name":"Western Mediterranean", 
     "id":"L34381", 
     "type":"CruiseArea", 
     "available":true, 
     "countries":"Spain", 
     "group":null 
    }, 
    { 
     "name":"Eastern Mediterranean", 
     "id":"L34373", 
     "type":"CruiseArea", 
     "available":false, 
     "countries":"Greece,Italy,Slovenia,Montenegro,Croatia", 
     "group":null 
    }, 
    { 
     "name":"North Africa And Middle East", 
     "id":"L04301", 
     "type":"CruiseArea", 
     "available":false, 
     "countries":"Morocco", 
     "group":null 
    }, 
    { 
     "name":"Fjords, Iceland And Arctic", 
     "id":"L34384", 
     "type":"CruiseArea", 
     "available":true, 
     "countries":"Norway", 
     "group":null 
    }, 
    { 
     "name":"Northern Europe And UK", 
     "id":"L34383", 
     "type":"CruiseArea", 
     "available":true, 
     "countries":"United Kingdom", 
     "group":null 
    } 
    ], 
    "countries":[ 
    { 
     "name":"Spain", 
     "id":"ESP", 
     "type":"COUNTRY", 
     "available":true, 
     "countries":null, 
     "group":null 
    }, 
    { 
     "name":"Jamaica", 
     "id":"JAM", 
     "type":"COUNTRY", 
     "available":false, 
     "countries":null, 
     "group":null 
    }, 
    { 
     "name":"Greece", 
     "id":"GRC", 
     "type":"COUNTRY", 
     "available":false, 
     "countries":null, 
     "group":null 
    }, 
    { 
     "name":"Italy", 
     "id":"ITA", 
     "type":"COUNTRY", 
     "available":false, 
     "countries":null, 
     "group":null 
    }, 
    { 
     "name":"Turkey", 
     "id":"TUR", 
     "type":"COUNTRY", 
     "available":false, 
     "countries":null, 
     "group":null 
    }, 
    { 
     "name":"Barbados", 
     "id":"BRB", 
     "type":"COUNTRY", 
     "available":false, 
     "countries":null, 
     "group":null 
    }, 
    { 
     "name":"Mexico", 
     "id":"MEX", 
     "type":"COUNTRY", 
     "available":false, 
     "countries":null, 
     "group":null 
    }, 
    { 
     "name":"Israel", 
     "id":"ISR", 
     "type":"COUNTRY", 
     "available":false, 
     "countries":null, 
     "group":null 
    }, 
    { 
     "name":"Cuba", 
     "id":"CUB", 
     "type":"COUNTRY", 
     "available":false, 
     "countries":null, 
     "group":null 
    }, 
    { 
     "name":"Croatia", 
     "id":"HRV", 
     "type":"COUNTRY", 
     "available":false, 
     "countries":null, 
     "group":null 
    } 
    ], 
    "itineraries":[], 
    "airportGroups":[] 
    }, 
"nomatch":false, 
"durations":null, 
"searchError":null 
} 
+0

使用[^「]''代替。+?' – hjpotter92

+2

而是使用JSON解析器像https://github.com/stig/json-framework – hjpotter92

+0

嗨..感謝..我得到了我想要的使用[^「] + – shett

回答

0

最簡單的方案(沒有總重寫)

樣本數據可以是

{"name":"[^"]*","id":"([^"]*)","type":"([^"]*)","available":true,[^{}]*} 

代替掃描的用於. - 任何字符,但換行符 - 掃描任何字符,但{} - [^{}]

See it here at regex101

+0

可以請你解釋我是如何工作的? – shett

+0

檢查regex101的說明。它非常徹底。 – ClasG

相關問題