content='[1502165009733][DEBUG][TID:140600116508416][asr_request_agent.cpp:219][ASRRequest::generate_asr_request]paramsjsonstring:{"pam":"IDPAAS","idx":"2","pdt":"4006","glb":"e3dece1a-057f-4abe-94bc-46ebde36f844","enc":"utf-8","rtn":"json","ptc":"101","app":"com.baidu.aip.huatuo","fun":"262400"}'#re.search()printre.findall(reg,content)正则是:^\[(\d+)]|glb\":\"(.*)\",\"enc|\[(\d+)]|idx\":\"(.*)",\"pdt可是为什么匹配的结果是[('1502165009733','','',''),('','','','2'),('','e3dece1a-057f-4abe-94bc-46ebde36f844','','')]数字里面有三个元素,而每个里面又有三个,但是想要的结果是['1502165009733','e3dece1a-057f-4abe-94bc-46ebde36f844','2']想问一下是正则错误了还是确实这样?
2 回答
扬帆大鱼
TA贡献1799条经验 获得超9个赞
importrereg='\[(\d+)\][\s\S]*?"idx":"(\d+)"[\s\S]*?"glb":"(.*?)"'content='[1502165009733][DEBUG][TID:140600116508416][asr_request_agent.cpp:219][ASRRequest::generate_asr_request]paramsjsonstring:{"pam":"IDPAAS","idx":"2","pdt":"4006","glb":"e3dece1a-057f-4abe-94bc-46ebde36f844","enc":"utf-8","rtn":"json","ptc":"101","app":"com.baidu.aip.huatuo","fun":"262400"}'g=re.search(reg,content)a,b,c=g.groups()printa,b,c
呼如林
TA贡献1798条经验 获得超3个赞
感觉你好像没想清楚要怎么用正则。目前贴出来了正则是4个部分或的关系,也就是说任何4个中的任何一个匹配,就会认为匹配到了。由于是findall,所以会从匹配结束点的位置继续向后找,看有没有和4个里的任何一个匹配的字符串。由于正则里有4个捕获组(),所以匹配结果会是个数组。而由于这4个组出于或关系的4段里,所以永远最多只会有一个匹配。我猜测你是想按照字符串顺序匹配这4部分,那么应该写作:^\[(\d+)].*idx\":\"(.*)\",\"pdt.*glb\":\"(.*)\",\"enc.*(?:\[(\d+)])?
添加回答
举报
0/150
提交
取消