已掉线,重新登录

首页 > 绿虎论坛 > 历史版块 > 编程 > 其他编程语言

标题: 正则大佬们,这段正则应该怎么匹配才能得到目标

作者: @Ta

时间: 2021-04-29

点击: 4729

今天没事看了下正则,随便抓了一段数据,想联系用正则提取

"scoreFlag": 1,
"scoreWeight": 1,
"score": 1.3,
"limitTime": 2000,
"testsNum": 100,
"gateContitionValue": "d15a105a-ccbd-462c-aa61-f9c8858a6224",
"testsType": 1,
"passContitionRequest": 2,
"passContitionValue": "90",
"state": 0,
"gateRepeat": 0,
"gateRepeatMarkWeight": 0.2,
"passFailureMarkWeight": 0.2,
"gateSuperId": "d15a105a-ccbd-462c-aa61-f9c8858a6224",
"mark": "",
"answerWrongFlag": 0,
"courseInfoIds": "",
"rushState": 2,
"crtUser": "admin001",
"crtTime": "2020-07-26 10:10:02",
"crtIp": "192.168.13.48",
"updTime": "2020-07-26 10:10:01",
"id": "e562edd3-49d6-459f-ab43-62e06551509b"
}
{
"scoreFlag": 1,
"scoreWeight": 1,
"score": 1.3,
"limitTime": 2000,
"testsNum": 100,
"gateContitionValue": "d15a105a-ccbd-462c-aa61-f9c8858a6224",
"testsType": 1,
"passContitionRequest": 2,
"passContitionValue": "90",
"state": 0,
"gateRepeat": 0,
"gateRepeatMarkWeight": 0.2,
"passFailureMarkWeight": 0.2,
"gateSuperId": "d15a105a-ccbd-462c-aa61-f9c8858a6224",
"mark": "",
"answerWrongFlag": 0,
"courseInfoIds": "",
"rushState": 2,
"crtUser": "admin001",
"crtTime": "2020-07-26 10:10:02",
"crtIp": "192.168.13.48",
"updTime": "2020-07-26 10:10:01",
"id": "e562edd3-49d6-459f-ab43-62e06551509b"
}
{
"scoreFlag": 1,
"scoreWeight": 1,
"score": 1.3,
"limitTime": 2000,
"testsNum": 100,
"gateContitionValue": "d15a105a-ccbd-462c-aa61-f9c8858a6224",
"testsType": 1,
"passContitionRequest": 2,
"passContitionValue": "90",
"state": 0,
"gateRepeat": 0,
"gateRepeatMarkWeight": 0.2,
"passFailureMarkWeight": 0.2,
"gateSuperId": "d15a105a-ccbd-462c-aa61-f9c8858a6224",
"mark": "",
"answerWrongFlag": 0,
"courseInfoIds": "",
"rushState": 2,
"crtUser": "admin001",
"crtTime": "2020-07-26 10:10:02",
"crtIp": "192.168.13.48",
"updTime": "2020-07-26 10:10:01",
"id": "e562edd3-49d6-459f-ab43-62e06551509b"
}

目标是提取到id:后面双引号里面的内容。

我用一下只能匹配到id一行的内容。不能一次得到里面的内容。

"id": "(.*)"

特来问问大佬们怎么提的
小米8(白)


[隐藏样式|查看源码]


『回复列表(8|隐藏机器人聊天)』

1.

这不是json么
小米8高配版(五颜六色)

(/@Ta/2021-04-29 22:09//)

2.

@HongKongDoll,是呀,用正则匹配。json提取我能提出来。只是想学明白正则方法。干扰太多。总时提不全
小米8(白)

(/@Ta/2021-04-29 22:17//)

3.

<?php

date_default_timezone_set('Asia/Shanghai');


$str = '
"scoreFlag": 1,
"scoreWeight": 1,
"score": 1.3,
"limitTime": 2000,
"testsNum": 100,
"gateContitionValue": "d15a105a-ccbd-462c-aa61-f9c8858a6224",
"testsType": 1,
"passContitionRequest": 2,
"passContitionValue": "90",
"state": 0,
"gateRepeat": 0,
"gateRepeatMarkWeight": 0.2,
"passFailureMarkWeight": 0.2,
"gateSuperId": "d15a105a-ccbd-462c-aa61-f9c8858a6224",
"mark": "",
"answerWrongFlag": 0,
"courseInfoIds": "",
"rushState": 2,
"crtUser": "admin001",
"crtTime": "2020-07-26 10:10:02",
"crtIp": "192.168.13.48",
"updTime": "2020-07-26 10:10:01",
"id": "e562edd3-49d6-459f-ab43-62e06551509b"
}
{
"scoreFlag": 1,
"scoreWeight": 1,
"score": 1.3,
"limitTime": 2000,
"testsNum": 100,
"gateContitionValue": "d15a105a-ccbd-462c-aa61-f9c8858a6224",
"testsType": 1,
"passContitionRequest": 2,
"passContitionValue": "90",
"state": 0,
"gateRepeat": 0,
"gateRepeatMarkWeight": 0.2,
"passFailureMarkWeight": 0.2,
"gateSuperId": "d15a105a-ccbd-462c-aa61-f9c8858a6224",
"mark": "",
"answerWrongFlag": 0,
"courseInfoIds": "",
"rushState": 2,
"crtUser": "admin001",
"crtTime": "2020-07-26 10:10:02",
"crtIp": "192.168.13.48",
"updTime": "2020-07-26 10:10:01",
"id": "e562edd3-49d6-459f-ab43-62e06551509b"
}
{
"scoreFlag": 1,
"scoreWeight": 1,
"score": 1.3,
"limitTime": 2000,
"testsNum": 100,
"gateContitionValue": "d15a105a-ccbd-462c-aa61-f9c8858a6224",
"testsType": 1,
"passContitionRequest": 2,
"passContitionValue": "90",
"state": 0,
"gateRepeat": 0,
"gateRepeatMarkWeight": 0.2,
"passFailureMarkWeight": 0.2,
"gateSuperId": "d15a105a-ccbd-462c-aa61-f9c8858a6224",
"mark": "",
"answerWrongFlag": 0,
"courseInfoIds": "",
"rushState": 2,
"crtUser": "admin001",
"crtTime": "2020-07-26 10:10:02",
"crtIp": "192.168.13.48",
"updTime": "2020-07-26 10:10:01",
"id": "e562edd3-49d6-459f-ab43-62e06551509b"
}
';

preg_match_all('/"id":."(.*)?"/',$str,$arr);

var_dump($arr);
array(2) {
 [0]=>
 array(3) {
   [0]=>
   string(44) ""id": "e562edd3-49d6-459f-ab43-62e06551509b""
   [1]=>
   string(44) ""id": "e562edd3-49d6-459f-ab43-62e06551509b""
   [2]=>
   string(44) ""id": "e562edd3-49d6-459f-ab43-62e06551509b""
 }
 [1]=>
 array(3) {
   [0]=>
   string(36) "e562edd3-49d6-459f-ab43-62e06551509b"
   [1]=>
   string(36) "e562edd3-49d6-459f-ab43-62e06551509b"
   [2]=>
   string(36) "e562edd3-49d6-459f-ab43-62e06551509b"
 }
}

(/@Ta/2021-04-29 22:55//)

4.

@艾木友尔尔巴,使用非贪婪模式,在*后跟?,采用最短匹配结果。默认是贪婪模式,采用最长匹配结果,所以会匹配到最后一个双引号为止。这应该是你遇到的问题。

$内容 = 'json文本';
preg_match('/"id":\s*"(.*?)"/', $内容, $arr);
var_dump($arr[1]);

和以下写法等价(U表示默认非贪婪模式):

'/"id":\s*"(.*)"/U'

或者也可以用否定断言:

/"id":\s*"([^"]*)"/

其中,[^"]*表示除双引号之外的其他字符,所以就不会匹配到多余的双引号了。

(/@Ta/2021-04-30 00:22//)

5.

@艾木友尔尔巴,更正,我看了你的描述,看起来你没有遇到贪婪问题,因为默认是按行匹配,一行只有一个值,所以默认的贪婪模式也没错。你的问题看起来是不知道怎么输出子组内容。很简单,preg_match()的匹配结果中,$arr[1]是第一个括号中的内容,$arr[2]是第二个括号中的内容,以此类推。所以要直接得到部分内容,在它周围打括号即可。$arr[0]是整个表达式匹配到的内容(一行的内容。)

(/@Ta/2021-04-30 00:20//)

6.

@老虎会游泳@大尨,谢谢。实验了一下。还是不瞎蒙了。后面系统的找个教程0基础上入门学习一下。
小米8(白)

(/@Ta/2021-04-30 12:35//)

7.

@艾木友尔尔巴,看这个

https://www.php.net/manual/zh/reference.pcre.pattern.syntax.php

点击目录链接查看内容。下方出现的英文是用户评论,不是内容。

(/@Ta/2021-04-30 12:51//)

8.

@老虎会游泳,好东西呀。感激不尽
小米8(白)

(/@Ta/2021-04-30 17:15//)

回复需要登录

7月5日 01:13 星期六

本站由hu60wap6驱动

备案号: 京ICP备18041936号-1