首页 > 编程 > Python > 正文

Python利用正则表达式匹配并截取指定子串及去重的方法

2019-11-25 17:06:50
字体:
来源:转载
供稿:网友

本文实例讲述了Python利用正则表达式匹配并截取指定子串及去重的方法。分享给大家供大家参考。具体如下:

import repattern=re.compile(r'/| (/d+) /| (/d+) /|')numset=set()all='''| 29266795 | 533 || 29370116 | 533 || 29467495 | 533 || 29500404 | 533 || 29500622 | 533 || 29515964 | 530 || 29516015 | 530 || 29520954 | 530 || 29520960 | 530 || 29525346 | 530 || 29525351 | 530 || 29525365 | 530 |'''matches=pattern.findall(all)for did,dt in matches: numset.add(dt)print numset

PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:

JavaScript正则表达式在线测试工具:
http://tools.VeVB.COm/regex/javascript

正则表达式在线生成工具:
http://tools.VeVB.COm/regex/create_reg

希望本文所述对大家的Python程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表