用正则表达式表达出linkPerson,mobile,vehicleNo,areas,引号和括号里的字符
发布网友
发布时间:2022-05-23 01:26
我来回答
共2个回答
热心网友
时间:2023-10-09 16:39
正则表达式:("linkPerson":".*?","mobile":".*?","vehicleNo":".*?",).*?,("areas":\[.*?\])
取第一捕获组和第二捕获组的数据就是你要的linkPerson,mobile,vehicleNo,areas的数据。
我给你一个php语言的例子,你看看吧。
<?php
$string='{"code":"0","message":"success","returnData":[{"vehicleId":"CFEEF42CE90E489E8784527D929DD0F8","linkPerson":"陈印栋","mobile":"13820072575","vehicleNo":"津AX5658","vehicleType":"56","length":12.5,"ratifyLoad":30.0,"areaCodes":null,"areas":["北京市","天津市","唐山市","沧州市","廊坊市"],"lat":39.02837,"utc":1434656165369,"verifiedVehicle":0,"familarVehicle":0,"verifiedUser":1,"pic":null,"roadGps":"天津市滨海新区海兴路","proxyGps":"天津市滨海新区","locationType":3,"contactTime":0,"isApp":"1","isGps":null,"isLbs":null,"isOnline":0,"label":{},"hasWaitDealOrder":0,"hasTransOrder":0,"selfDriverFlag":0,"called":0,"lon":117.573962}],"total":1,"serviceTime":1434971777661}';
$pattern = '/("linkPerson":".*?","mobile":".*?","vehicleNo":".*?",).*?,("areas":\[.*?\])/i';
preg_match_all($pattern,$string,$matches);
echo $matches[1][0].$matches[2][0];
?>
运行结果:
"linkPerson":"陈印栋","mobile":"13820072575","vehicleNo":"津AX5658","areas":["北京市","天津市","唐山市","沧州市","廊坊市"]
热心网友
时间:2023-10-09 16:39
我不知道理解了你的问题没,我描述一下:
你是想,假如returnData里数据很多的话,如何获取是吗?
比如(下面这个还是你的数据,只是returnData我复制了一段,其中手机号改了一下)
{"code":"0","message":"success","returnData":[{"vehicleId":"CFEEF42CE90E489E8784527D929DD0F8","linkPerson":"陈印栋","mobile":"13820072575","vehicleNo":"津AX5658","vehicleType":"56","length":12.5,"ratifyLoad":30.0,"areaCodes":null,"areas":["北京市","天津市","唐山市","沧州市","廊坊市"],"lat":39.02837,"utc":1434656165369,"verifiedVehicle":0,"familarVehicle":0,"verifiedUser":1,"pic":null,"roadGps":"天津市滨海新区海兴路","proxyGps":"天津市滨海新区","locationType":3,"contactTime":0,"isApp":"1","isGps":null,"isLbs":null,"isOnline":0,"label":{},"hasWaitDealOrder":0,"hasTransOrder":0,"selfDriverFlag":0,"called":0,"lon":117.573962},{"vehicleId":"CFEEF42CE90E489E8784527D929DD0F8","linkPerson":"陈印栋","mobile":"13913913913","vehicleNo":"津AX5658","vehicleType":"56","length":12.5,"ratifyLoad":30.0,"areaCodes":null,"areas":["北京市","天津市","唐山市","沧州市","廊坊市"],"lat":39.02837,"utc":1434656165369,"verifiedVehicle":0,"familarVehicle":0,"verifiedUser":1,"pic":null,"roadGps":"天津市滨海新区海兴路","proxyGps":"天津市滨海新区","locationType":3,"contactTime":0,"isApp":"1","isGps":null,"isLbs":null,"isOnline":0,"label":{},"hasWaitDealOrder":0,"hasTransOrder":0,"selfDriverFlag":0,"called":0,"lon":117.573962}],"total":1,"serviceTime":1434971777661}
假如你是用js,可以这么做:
var p = /"mobile":".*?"/;
var s = str.match(p);
alert(s+""+"==="+s[0]+"==="+s[1]+"===");
这是因为我们知道手机的length是2,如果是动态的,则可以循环s.length来遍历