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,正则应该怎么写? 展开
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,正则应该怎么写? 展开
2个回答
展开全部
完整代码: #!/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搜标题,即可找到帖子地址)
"""
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搜标题,即可找到帖子地址)
展开全部
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
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
刚刚调试了一下。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询