python正则表达式匹配字符串的问题?

importretxt="bn','dd_ff');xm_a([1,'zhangming',0,2,'yuwenjige','lishijige',1,0,3,1,0,0... import re
txt ="bn','dd_ff');xm_a([1,'zhangming',0,2,'yuwenjige','lishijige',1,0,3,1,0,0.999,1,1,0,0]);xm_a([2,'wangmeng',0,2,'shuxuejige','dilijige',1,0,3,1,0,0.999,1,1,1,0]);xm_a([3,'wangli',0,2,'shuxuejige','dilijige',1,0,2,0,0,0.999,1,1,1,0]);zuobi(6,3,'4:5');fg_gh('xxx','vb_4');xm_a([4,'dashan',0,2,'huaxuejige','yingyujige',1,0,3,1,1,0.999,0,1,0,0]);</"
jige = re.findall("???怎么写???", txt)
print jige
>>>["zhangming", "wangmeng", "dashan"]

txt是有规律的,每句以xm_a([1开始,分号结束。用正则匹配xm_a([3,'wangli'里面的姓名,但xm_a([3,'wangli...;后面紧跟着这种特征的字符串zuobi(6,3,'4:5');则不匹配前面的wangli,正则应该怎么写?
展开
 我来答
againinput4
2012-12-09 · TA获得超过2407个赞
知道大有可为答主
回答量:1446
采纳率:70%
帮助的人:642万
展开全部
完整代码: #!/usr/bin/python# -*- coding: utf-8 -*-
"""
Function:
python正则表达式匹配字符串的问题?

Author: Crifan LiVersion: 2012-12-08
Contact: admin at crifan dot com
"""

import re;
txt ="bn','dd_ff');xm_a([1,'zhangming',0,2,'yuwenjige','lishijige',1,0,3,1,0,0.999,1,1,0,0]);xm_a([2,'wangmeng',0,2,'shuxuejige','dilijige',1,0,3,1,0,0.999,1,1,1,0]);xm_a([3,'wangli',0,2,'shuxuejige','dilijige',1,0,2,0,0,0.999,1,1,1,0]);zuobi(6,3,'4:5');fg_gh('xxx','vb_4');xm_a([4,'dashan',0,2,'huaxuejige','yingyujige',1,0,3,1,1,0.999,0,1,0,0]);</";
#注:此处用到了 前向否定匹配#不了解的,可以参考:
#【教程】详解Python正则表达式之: (?!…) negative lookahead assertion 前向否定匹配 /前向否定断言
jige = re.findall("xm_a\(\[\d+,'(\w+)',[^\(\)\[\]]+\]\);(?!zuobi)", txt);
print jige; #['zhangming', 'wangmeng', 'dashan']

另外,感兴趣的,可以去看我写的系列的教程:
【教程】详解Python正则表达式

(此处不给贴地址,请自己用google搜标题,即可找到帖子地址)
bdwisyou7d
2012-12-09 · TA获得超过1.3万个赞
知道大有可为答主
回答量:3774
采纳率:81%
帮助的人:1072万
展开全部
jige=[]
allmatch=findall("(?isu)xm_a\(\[\d+,'([^']+)?'),txt)
zuobi=findall("(?isu)xm_a\(\[\d+,'([^']+)?'.*?\);zuobi),txt)
if allmatch:
for name in allmatch:
if zuobi and name in zuobi:continue
jige.append(name)
print jige
更多追问追答
追问
zuobi这句正则有问题吧?我试了一下结果不对
...
allmatch=re.findall("(?isu)xm_a\(\[\d+,'([^']+)?'",txt)
zuobi=re.findall("(?isu)xm_a\(\[\d+,'([^']+)?'.*?\);zuobi",txt)
if allmatch:
for name in allmatch:
if zuobi and name in zuobi:continue
jige.append(name)
print jige
>>>['wangmeng', 'wangli', 'dashan']

应该是>>>["zhangming", "wangmeng", "dashan"]
追答
#python2.7
import re
txt ="bn','dd_ff');xm_a([1,'zhangming',0,2,'yuwenjige','lishijige',1,0,3,1,0,0.999,1,1,0,0]);"+\
"xm_a([2,'wangmeng',0,2,'shuxuejige','dilijige',1,0,3,1,0,0.999,1,1,1,0]);"+\
"xm_a([3,'wangli',0,2,'shuxuejige','dilijige',1,0,2,0,0,0.999,1,1,1,0]);zuobi(6,3,'4:5');fg_gh('xxx','vb_4');"+\
"xm_a([4,'dashan',0,2,'huaxuejige','yingyujige',1,0,3,1,1,0.999,0,1,0,0]);</";
jige=[]
allmatch=re.findall("(?isu)xm_a\(\[\d+,'([^']+)?'",txt)
zuobi=re.findall("(?isu)xm_a\(\[\d+,'([^']+)?'[^\)]*?\);zuobi",txt)
if allmatch:
for name in allmatch:
print name
if zuobi and name in zuobi:continue
jige.append(name)
print jige

刚刚调试了一下。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式