2015-12-07 142 views
1

更換分組圖案我有,我想用PHP從字符串數組取代「Systems_x0020_Changed_IDs」值的JSON文件。 「39122」變成[39122],並且「39223,39244,39395」變成[39223,39244,39395]。我正在使用http://www.regexpal.com/來測試我的表情。表達式是:問題與PHP的正則表達式

"([(0-9)+((, *))]+)+" 

這在PHP中產生了意想不到的結果。在我的JSON文件:

[{ 
     "ID": 1050436, 
     "Title": "THE SKY IS FALLING!!!!", 
     "Application_x0020_ID": 242, 
     "Systems_x0020_Changed": "Academic Planning System (APS),\"Documents planning and evaluation processes at UGA that support cont", 
     "Systems_x0020_Changed_IDs": "39122", 
     "Status": "New", 
     "Modified": "2015-10-28T16:14:45.573-04:00", 
     "Age": 40, 
     "Description_x0020__x0028_Public_x0029_": "I'm chicken little and the SKY IS FALLING!", 
     "Impact_x0020__x0028_Public_x0029_": "The world is going to end!", 
     "Start_x0020_Time": "2015-10-28T00:00:00-04:00", 
     "End_x0020_Time": "2015-10-30T00:00:00-04:00", 
     "Hours": 12 
    }, { 
     "ID": 1050740, 
     "Title": "This is a Title", 
     "Application_x0020_ID": 242, 
     "Systems_x0020_Changed": "EITS Websites,\"EITS departmental web pages.\", GACRC Archival Storage,\"Archival Storage for Research Data\", VPS,\"Mainframe distributed printing system\"", 
     "Systems_x0020_Changed_IDs": "39223, 39244, 39395", 
     "Status": "New", 
     "Modified": "2015-11-05T17:31:13.15-05:00", 
     "Age": 32, 
     "Description_x0020__x0028_Public_x0029_": "We will tell jokes to the clients", 
     "Impact_x0020__x0028_Public_x0029_": "Everyone will notice the change.", 
     "Start_x0020_Time": "2015-11-27T08:38:00-05:00", 
     "End_x0020_Time": "2015-11-30T00:00:00-05:00", 
     "Hours": 1 
    }]

在線路末端的幾個逗號被替換括號[],使輸出的樣子:

[{ 
    "ID": 1050436, 
    "Title": "THE SKY IS FALLING!!!![,]Application_x0020_ID": 242, 
    "Systems_x0020_Changed": "Academic Planning System (APS),\"Documents planning and evaluation processes at UGA that support cont[,]Systems_x0020_Changed_IDs": 39122, 
    "Status": "New[,]Modified": "2015-10-28T16:14:45.573-04:00[,]Age": 40, 
    "Description_x0020__x0028_Public_x0029_": "I'm chicken little and the SKY IS FALLING![,]Impact_x0020__x0028_Public_x0029_": "The world is going to end![,]Start_x0020_Time": "2015-10-28T00:00:00-04:00[,]End_x0020_Time": "2015-10-30T00:00:00-04:00[,]Hours": 12 
}, { 
    "ID": 1050740, 
    "Title": "This is a Title[,]Application_x0020_ID": 242, 
    "Systems_x0020_Changed": "EITS Websites,\"EITS departmental web pages.\", GACRC Archival Storage,\"Archival Storage for Research Data\", VPS,\"Mainframe distributed printing system\"[,]Systems_x0020_Changed_IDs": [39223, 39244, 39395], 
    "Status": "New[,]Modified": "2015-11-05T17:31:13.15-05:00[,]Age": 32, 
    "Description_x0020__x0028_Public_x0029_": "We will tell jokes to the clients[,]Impact_x0020__x0028_Public_x0029_": "Everyone will notice the change.[,]Start_x0020_Time": "2015-11-27T08:38:00-05:00[,]End_x0020_Time": "2015-11-30T00:00:00-05:00[,]Hours": 1 
}]

我的問題是,我怎麼能修改表達式所以PHP的行爲就像regexpal.com一樣,只有引號內的數字並忽略其餘部分?

+1

修改與正則表達式結構化數據像JSON是自找麻煩。我建議用'json_decode'將它讀入適當的結構中,修改結構並用'json_encode'將其寫回。 –

回答

2

你的正則表達式很奇怪,你似乎試圖在字符類[...]中放置一個模式表達式,這可能不會達到你所期望的。此外,你的regex會匹配其他鍵/值對內的值。試試這個,這隻會爲重點匹配值「Systems_x0020_Changed_IDs」:

"Systems_x0020_Changed_IDs":\s+"([^"]*)" 
+1

@ miken32'\ d +'將在'「Systems_x0020_Changed_IDs」失敗:「39223,39244,39395」' – chris85

+0

@ miken32 - 這可能是真的,但帖子肯定不會說的想只匹配數字什麼,所以我沒有對該領域的內容做任何假設。 –

1

什麼只是解析它,它是JSON?

$jsons = array('{ 
     "ID": 1050436, 
     "Title": "THE SKY IS FALLING!!!!", 
     "Application_x0020_ID": 242, 
     "Systems_x0020_Changed": "Academic Planning System (APS),\"Documents planning and evaluation processes at UGA that support cont", 
     "Systems_x0020_Changed_IDs": "39122", 
     "Status": "New", 
     "Modified": "2015-10-28T16:14:45.573-04:00", 
     "Age": 40, 
     "Description_x0020__x0028_Public_x0029_": "I\'m chicken little and the SKY IS FALLING!", 
     "Impact_x0020__x0028_Public_x0029_": "The world is going to end!", 
     "Start_x0020_Time": "2015-10-28T00:00:00-04:00", 
     "End_x0020_Time": "2015-10-30T00:00:00-04:00", 
     "Hours": 12 
    }', '{ 
     "ID": 1050740, 
     "Title": "This is a Title", 
     "Application_x0020_ID": 242, 
     "Systems_x0020_Changed": "EITS Websites,\"EITS departmental web pages.\", GACRC Archival Storage,\"Archival Storage for Research Data\", VPS,\"Mainframe distributed printing system\"", 
     "Systems_x0020_Changed_IDs": "39223, 39244, 39395", 
     "Status": "New", 
     "Modified": "2015-11-05T17:31:13.15-05:00", 
     "Age": 32, 
     "Description_x0020__x0028_Public_x0029_": "We will tell jokes to the clients", 
     "Impact_x0020__x0028_Public_x0029_": "Everyone will notice the change.", 
     "Start_x0020_Time": "2015-11-27T08:38:00-05:00", 
     "End_x0020_Time": "2015-11-30T00:00:00-05:00", 
     "Hours": 1 
    }'); 
foreach($jsons as $json){ 
    $json_array = json_decode($json, true); 
    echo $json_array['Systems_x0020_Changed_IDs'] . "\n"; 
} 

演示:https://eval.in/481865

如果你需要一個正則表達式,你可以這樣做:

"Systems_x0020_Changed_IDs":\h*"(([\d+],?\h*)*)" 

演示:https://regex101.com/r/yZ6eM3/1

PHP用法:

$string = '{ 
     "ID": 1050436, 
     "Title": "THE SKY IS FALLING!!!!", 
     "Application_x0020_ID": 242, 
     "Systems_x0020_Changed": "Academic Planning System (APS),\"Documents planning and evaluation processes at UGA that support cont", 
     "Systems_x0020_Changed_IDs": "39122", 
     "Status": "New", 
     "Modified": "2015-10-28T16:14:45.573-04:00", 
     "Age": 40, 
     "Description_x0020__x0028_Public_x0029_": "I\'m chicken little and the SKY IS FALLING!", 
     "Impact_x0020__x0028_Public_x0029_": "The world is going to end!", 
     "Start_x0020_Time": "2015-10-28T00:00:00-04:00", 
     "End_x0020_Time": "2015-10-30T00:00:00-04:00", 
     "Hours": 12 
    }, { 
     "ID": 1050740, 
     "Title": "This is a Title", 
     "Application_x0020_ID": 242, 
     "Systems_x0020_Changed": "EITS Websites,\"EITS departmental web pages.\", GACRC Archival Storage,\"Archival Storage for Research Data\", VPS,\"Mainframe distributed printing system\"", 
     "Systems_x0020_Changed_IDs": "39223, 39244, 39395", 
     "Status": "New", 
     "Modified": "2015-11-05T17:31:13.15-05:00", 
     "Age": 32, 
     "Description_x0020__x0028_Public_x0029_": "We will tell jokes to the clients", 
     "Impact_x0020__x0028_Public_x0029_": "Everyone will notice the change.", 
     "Start_x0020_Time": "2015-11-27T08:38:00-05:00", 
     "End_x0020_Time": "2015-11-30T00:00:00-05:00", 
     "Hours": 1 
    }'; 
$regex = '/"Systems_x0020_Changed_IDs":\h*"((?:[\d+],?\h*)*)"/'; 
preg_match_all($regex, $string, $matches); 
print_r($matches[1]); 

輸出:

​​

演示#2:https://eval.in/481871

0

我一直在尋找的答案是:

$str = preg_replace('/"((\d+[, ]*)+)"/', "[$1]", $str); 

我需要的JSON文件是除了數值爲字符串。我的正則表達式工作後,我再玩一點。

+0

是不是我在答案的第二部分給出的正則表達式,不是那麼嚴格? '(([\ d +],?\ h *)*)' – chris85

+0

可能是。我甚至沒有注意,因爲你把Systems_x0020_Changed_IDs添加到它的前面,這不是我想要做的。不過謝謝你的幫助! –

+0

現在我正在看你的正則表達式,我發現它可能是一個更好的解決方案。謝謝您的幫助! –