2015-12-07 99 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

现在我正在看你的正则表达式,我发现它可能是一个更好的解决方案。谢谢您的帮助! –