import re
from re import findall,search,S secret_code = 'hadkfalifexxIxxfasdjifja134xxlovexx23345sdfxxyouxx8dfse' #.的使用,匹配任意字符,换行符除外
a = 'abc123'
b = re.compile('a...')
c = b.findall(a)
print c #*的使用,匹配前一个字符0次或者多次
a1 = 'abababb123abbbbqjlejl23abbb'
b1 = re.compile('a*b')
c1 = b.findall(a1)
print(c1) #?的使用,匹配前一个字符0次或者1次
a = 'wqebasd123qewea'
b = re.compile('w?e')
c = b.findall(a)
print(c) #.*组合使用,贪婪匹配
b = re.compile('xx.*xx')
c = b.findall(secret_code)
print(c)
#.*?组合使用
b = re.compile('xx.*?xx')
c = b.findall(secret_code)
print(c) #使用括号与不使用括号的差别
b = re.compile('xx(.*?)xx')
c = b.findall(secret_code)
print(c) 输出结果:
['abc1']
['abab', 'abb1', 'abbb', 'abbb']
['e', 'e', 'we']
['xxIxxfasdjifja134xxlovexx23345sdfxxyouxx']
['xxIxx', 'xxlovexx', 'xxyouxx']
['I', 'love', 'you']
import re
#匹配文本中的邮箱
y='[email protected]@[email protected]@adfcom'
ret = re.findall('\w+@(?:qq|163|126).com',y)
print(ret)
#匹配文本中的时间字符串
time='asfasf1990-07-12asdfAAAbbbb434241' ret1 = re.search(r'(?P<year>19[09]\d)-(?P<month>\d+)-(?P<days>\d+)',time)
print(ret1.group('year'))
print(ret1.group('month'))
print(ret1.group('days'))
#匹配文本中的所有的身份证数字
a='sfafsf,34234234234,1231313132,154785625475896587,sdefgr54184785ds85,4864465asf86845'
ret2=re.findall('\d{18}',a)
print(ret2) #匹配出所有整数
a = '1,-3,3,-5,6,7.3,ad,ae,-98'
ret3 = re.compile(r"'(-?\d+)'")
result = ret3.findall(str(re.split(',',a)))
print(result) 输出结果:
['[email protected]', '[email protected]', '[email protected]']
1990
07
12
['']
['', '-3', '', '-5', '', '-98']
05-11 05:20