Skip to content

Instantly share code, notes, and snippets.

@zhoufankai
Last active February 7, 2017 16:54
Show Gist options
  • Save zhoufankai/5add5c56a298f65fd4edd995daad3425 to your computer and use it in GitHub Desktop.
Save zhoufankai/5add5c56a298f65fd4edd995daad3425 to your computer and use it in GitHub Desktop.
# encoding: utf8
# 正则表达式使用
import re
# 首先编译你要查找的类型
p = re.compile('\d+')
# 查看开始能否匹配到
m = p.match("1233,4,bb,22")
print m.group()
# 1233 int
# 查找全部匹配到的数字
f = p.findall("1233,4,bb,22")
print f
# ['1233', '4', '22'] str
# 通过finditer迭代来查找数字的范围,注意是范围,不是全部下标
>>> iterator = p.finditer('12 drummers drumming, 11 ... 10 ...')
>>> iterator
<callable-iterator object at 0x...>
>>> for match in iterator:
... print match.span()
...
(0, 2)
(22, 24)
(29, 31)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment