用PHP正则表达式替换分组模式时出现的问题


Problems replacing grouped patterns with PHP regex

我有一个JSON文件,我想用PHP将字符串中的"Systems_x0020_Changed_ID"值替换为数组。"39122"变为[39122],"39223、39244、39395"变为[39223、39.44、39395]。我正在使用http://www.regexpal.com/测试我的表情。表达式为:

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

这在PHP中产生了意想不到的结果。在我的JSON文件中:

[{"ID":1050436,"标题":"天塌了!!!","Application_x0020_ID":242,"Systems_x0020_Changed":"学术规划系统(APS),''"教资会支持cont的规划和评估过程文件","Systems_x0020_Changed_ID":"39122","状态":"新建","修改":"2015-10-28T16:14:45.573-04:00","年龄":40岁,"Description_x0020_x0028_Public_x0029_":"我是小鸡,天塌下来了!","Impact_x0020_x0028_Public_x0029_":"世界要结束了!","Start_x0020_Time":"2015-10-28T00:00:00-04:00","End_x0020_Time":"2015-10-30T00:00-04:00","小时":12}{"ID":1050740,"标题":"这是标题","Application_x0020_ID":242,"Systems_x0020_Changed":"EITS网站,''"EITS部门网页''",GACRC档案存储,''"研究数据档案存储''",VPS,''"大型机分布式打印系统''","Systems_x0020_Changed_ID":"39223、39244、39395","状态":"新建","修改":"2015-11-05T17:31:13.15-05:00","年龄":32岁,"Description_x0020_x0028_Public_x0029_":"我们会给客户讲笑话","Impact_x0020_x0028_Public_x0029_":"每个人都会注意到变化。","Start_x0020_Time":"2015-11-27T08:0-05:00","End_x0020_Time":"2015-11-30T00:00-05:00","小时":1}]

行末的几个逗号被括号[]替换,因此输出看起来像:

[{"ID":1050436,"标题":"天塌了!!![,]Application_x0020_ID":242,"Systems_x0020_Changed":"学术规划系统(APS),''"教资会支持cont[,]Systems_x0202_Changed_ID的文件规划和评估过程":39122,"状态":"新建[,]修改":"2015-10-28T16:14:45.573-04:00[,]年龄":40,"Description_x0020_x0028_Public_x0029_":"我是小鸡,天空在坠落![,]Impact_x0020_x0028_Ppublic_x0029_[":"世界要结束了![,]Start_x0020_Time":"2015-10-28T00:00:00-04:00[,]end_x0020_Time":"2015-10-30T000:00:00-0400[,]Hours":12}{"ID":1050740,"标题":"这是一个标题[,]Application_x0020_ID":242,"Systems_x0020_Changed":"EITS网站,''"EITS部门网页''",GACRC档案存储,''"研究数据档案存储''",VPS,''"大型机分布式打印系统''"[,]Systems_x0020_Changed_ID":[39223,39244,39395],"状态":"新[,]修改":"2015-11-05T17:31:13.15-05:00[,]年龄":32,"Description_x0020_x0028_Public_x0029_":"我们会给客户讲笑话[,]Impact_x0020_x0028_Ppublic_x0029_]":"每个人都会注意到变化。[,]Start_x0020_Time":"2015-11-27T00:00-05:00[,]End_x0020_Time":}]

我的问题是,如何修改表达式,使PHP的行为像regexpal.com,只获取引号中的数字,而忽略其余数字?

您的正则表达式相当奇怪,您似乎试图将模式表达式放入字符类[...]中,这可能没有达到您预期的效果。此外,您的正则表达式将匹配其他键/值对中的值。请尝试此操作,它将只匹配密钥"Systems_x0020_Changed_ID"的值:

"Systems_x0020_Changed_IDs":'s+"([^"]*)"

将其解析为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]);

输出:

Array
(
    [0] => 39122
    [1] => 39223, 39244, 39395
)

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

我想要的答案是:

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

除了数字值作为字符串之外,我需要JSON文件。我的正则表达式在我多玩了一点之后就起作用了。