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

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-10 15:08:23
Original
1964 people have browsed it

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

import re
pattern=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

Copy after login

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

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template