ITPub博客

首页 > 应用开发 > > Python学习-re模块findall search

Python学习-re模块findall search

作者:蚂蚁没问题 时间:2019-09-11 16:15:15 0 删除 编辑

[root@localhost /]# cat a.py 

#!/bin/python

import re

s = '1102231990xxxxxxxx'

res = re.search('(?P\d{3})(?P\d{3})(?P\d{4})',s)

ret = re.findall('(?P\d{3})(?P\d{3})(?P\d{4})',s)

print(res.groupdict())

print(ret)

[root@localhost /]# 

数量词{1,2}

非贪婪{1,2}?

* 号 前面的字符匹配0次或无限多次

+号 前面的字符匹配一次或无限多次

?号前面的字符匹配0次或一次

边界匹配^    $

以上都是指的字符,下面介绍组

组的概念()

[root@localhost /]# cat str.py 

#!/bin/python

import re

a='python 1111java678php'

b=re.findall('[a-z]{3,6}?',a)

print b

c='pytho0python1pythonn2'

d=re.findall('python*',c)

e=re.findall('python+',c)

f=re.findall('python?',c)

g=re.findall('python{1,2}?',c)

print d

print e

print f

print g

u='123456'

h=re.findall('^\d{4,8}$',u)

print h

p='pythonpythonpython'

q=re.findall('(python){2}',p)

print q

[root@localhost /]# 

#!/bin/python

import re

a='python 1111yyyyjava222php'

b='pytho0python1pythonn2'

print re.findall('[a-z]',a)

print re.findall('[a-z]{3}',a)

print re.findall('[a-z]{3,6}',a)

print re.findall('[a-z]{3,6}?',a)

print re.findall('python*',b)

print re.findall('python+',b)

print re.findall('python?',b)

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2568/viewspace-3149817/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-16

  • 博文量
    107
  • 访问量
    675