re模块的使用

#re模块
#常用的方法
compile(pattern, flags = 0)        匹配任何可选的标记来编译正则表达式的模式,然后返回一个正则表达式对象
match(pattern, string, flags = 0)     使用带有可选标记的正则表达式的模式来匹配字符串。如果匹配成功,返回匹配对象,否则返回None
search(pattern, string ,flags = 0)     使用可选标记搜索字符串中第一次出现的正则表达式模式。如果匹配成功,则返回匹配对象,否则返回None
findall(pattern, string[,flags] )          查找字符串中所有(非重复)出现的正则表达式模式,并返回一个匹配列表
finditer(pattern, string[,flags] )        与findall()相同,但返回的是一个迭代器。对于每一次匹配,迭代器都能返回一个匹配对象
split(pattern, string, max = 0)         根据正则表达式的模式分隔符,split函数将字符串分割为列表,返回匹配列表,分割最多操作max次
group(num = 0)               返回整个匹配对象,或者编号为num的特定组

import re
m = re.search('foo','asdasdfooasd')
#这里如果使用match将匹配不到任何字符串,因为match从第一个a开始匹配if m is not None:
print(m.group())
regex = <(.*)>(.*)<\/(.*)>|<(.*)\/>
m = re.search(regex,"aa<a>aaaa</a>")
#一样只有search能匹配到标签if m is not None:
print(m.group())

regex = '(foo\w)(\w)'
m = re.match(r'(foo\w)(\w)','fooasdfooasd')
if m is not None:
print(m.group(1))
print(m.groups())
#输出
#fooa
#('fooa', 's')

regex = 'apple'
m = re.findall(regex,'apple1 apple2 apple3')
print(m)
#输出
#['apple', 'apple', 'apple']
regex = 'apple'
m = [ g.group() for g in re.finditer(regex,'apple1 apple2 apple3')]
print(m)
#输出
#['apple', 'apple', 'apple']

list = [
'aaa, bbb ccc',
'ddd, eee fff',
]
for i in list:
print(re.split(', |(?= (?:[a-z]{3})) ',i))
#输出
#['aaa', 'bbb', 'ccc']
#['ddd', 'eee', 'fff']

 
re模块小实例:
__author__ = 'cq'

import re
from random import randrange,choice,randint
from string import ascii_lowercase as lc
from time import ctime


#生成数据文件def generate_data():
with open('./data.txt','w') as f:
for i in range(randint(20,30)):
tlds = ('com', 'edu', 'net', 'org', 'gov')
dtint = randint(100000000,1200000000) #生成时间戳
dtstr = ctime(dtint) #将时间戳转化为特定时间格式
llen = randrange(4, 8) #用户名长度
login = ''.join(choice(lc) for i in range(llen)) #生成用户名
dlen = randrange(llen,13) #域名长度
dom = ''.join(choice(lc) for i in range(dlen)) #生成域名
data_line = "%s::%s@%s.%s::%d-%d-%d\n" % (dtstr, login, dom, choice(tlds), dtint, llen, dlen)
f.write(data_line) #写入文件
print(data_line) #打印每行记录



#匹配指定日期的行def match_date():
regex = '(Mon|Tue|Wed|Thu|Fri|Sat|Sun)(.*)'
with open('./data.txt','r') as f:
m = re.findall(regex,f.read())
for i in m:
print(i)




#匹配在某时间段内的记录def match_time_slot():
regex = ' ([0-9]{1,2}) .*([0-9]{4})::(.*)'
# regex = ' ([0-9]{0,2}).*(::)(.*) '
with open('./data.txt','r') as f:
m = re.findall(regex,f.read())
for i in m:
if 2000 <= int(i[1]) and int(i[1]) <= 2020 and 20 <= int(i[0]) and int(i[0]) <= 31:
print(i)


#匹配某名单中人员的记录def match_name():
regex = '::([a-z]{2,13})@([a-z]{2,13})\.(com|edu|net|org|gov)'
with open('./data.txt','r') as f:
m = re.findall(regex,f.read())
for i in m:
print(i)



def main():
    generate_data()
    print("\n---------------match_date--------------------\n")
    match_date()
    print("\n---------------match_time_slot--------------------\n")
    match_time_slot()
    print("\n---------------match_name--------------------\n")
    match_name()


if '__main__' == __name__:
    main()

运行结果

Sun Mar  5 00:55:55 1989::qvnc@ygeowwaf.com::605033755-4-8

Mon Oct 17 17:16:31 2005::yene@rtewqvvyfe.edu::1129540591-4-10

Tue Oct  7 06:33:30 2003::wlyi@coagmnososzy.edu::1065479610-4-12

Mon Oct 16 00:01:06 2006::zsgok@jkpiplcm.edu::1160928066-5-8

Wed Mar 15 06:37:35 2000::paok@anpekysphicu.com::953073455-4-12

Wed Mar 26 12:27:25 1980::bodqoe@iydohek.org::322892845-6-7

Mon Jun  5 13:54:28 1989::fgiy@oppcjnafx.gov::613029268-4-9

Sun Jul 25 05:27:23 2004::agmljfx@qvxgjqtkiwnl.org::1090704443-7-12

Mon Nov 14 16:15:36 2005::tctz@bcikib.gov::1131956136-4-6

Sun Jan 14 23:20:42 2007::qqlfkf@isslbh.com::1168788042-6-6

Sun Jul 27 02:00:13 1980::cpiqwau@drbpfsfglip.edu::333482413-7-11

Sun Feb 20 16:10:34 2005::aguqfd@hnrcaged.com::1108887034-6-8

Wed Jun 27 06:13:05 1979::kowyk@ruoackjavkpq.net::299283185-5-12

Wed Oct 12 19:52:54 1994::kqaol@mzewoas.edu::781962774-5-7

Thu Aug 23 01:46:59 1973::uofpdq@zdeidbobin.org::114889619-6-10

Sat Dec 21 11:36:20 1991::hodw@wfbw.org::693286580-4-4

Tue Jun 22 14:42:19 1993::azgagm@nfmguh.org::740731339-6-6

Sun Feb 23 04:50:57 2003::cysfu@fnzdo.com::1045947057-5-5

Fri Jun 10 13:38:02 1983::qdhqw@fcdsvlmnhx.net::424071482-5-10

Sat Jan 24 21:56:37 1998::dfyicjw@fklbymd.org::885650197-7-7

Sun Jun  3 07:48:45 2007::wptuyjk@tsngnle.edu::1180828125-7-7

Mon Nov 19 00:34:41 2001::ocjlb@nusyk.net::1006101281-5-5

Sat Dec  1 21:01:23 1973::bvhx@lmir.net::123598883-4-4

Sun Dec 16 17:42:51 1979::rpgs@hppau.org::314185371-4-5

Mon Jul 21 23:46:13 1986::fnsro@nmbcwdmie.org::522344773-5-9


---------------match_date--------------------

('Sun', ' Mar  5 00:55:55 1989::qvnc@ygeowwaf.com::605033755-4-8')
('Mon', ' Oct 17 17:16:31 2005::yene@rtewqvvyfe.edu::1129540591-4-10')
('Tue', ' Oct  7 06:33:30 2003::wlyi@coagmnososzy.edu::1065479610-4-12')
('Mon', ' Oct 16 00:01:06 2006::zsgok@jkpiplcm.edu::1160928066-5-8')
('Wed', ' Mar 15 06:37:35 2000::paok@anpekysphicu.com::953073455-4-12')
('Wed', ' Mar 26 12:27:25 1980::bodqoe@iydohek.org::322892845-6-7')
('Mon', ' Jun  5 13:54:28 1989::fgiy@oppcjnafx.gov::613029268-4-9')
('Sun', ' Jul 25 05:27:23 2004::agmljfx@qvxgjqtkiwnl.org::1090704443-7-12')
('Mon', ' Nov 14 16:15:36 2005::tctz@bcikib.gov::1131956136-4-6')
('Sun', ' Jan 14 23:20:42 2007::qqlfkf@isslbh.com::1168788042-6-6')
('Sun', ' Jul 27 02:00:13 1980::cpiqwau@drbpfsfglip.edu::333482413-7-11')
('Sun', ' Feb 20 16:10:34 2005::aguqfd@hnrcaged.com::1108887034-6-8')
('Wed', ' Jun 27 06:13:05 1979::kowyk@ruoackjavkpq.net::299283185-5-12')
('Wed', ' Oct 12 19:52:54 1994::kqaol@mzewoas.edu::781962774-5-7')
('Thu', ' Aug 23 01:46:59 1973::uofpdq@zdeidbobin.org::114889619-6-10')
('Sat', ' Dec 21 11:36:20 1991::hodw@wfbw.org::693286580-4-4')
('Tue', ' Jun 22 14:42:19 1993::azgagm@nfmguh.org::740731339-6-6')
('Sun', ' Feb 23 04:50:57 2003::cysfu@fnzdo.com::1045947057-5-5')
('Fri', ' Jun 10 13:38:02 1983::qdhqw@fcdsvlmnhx.net::424071482-5-10')
('Sat', ' Jan 24 21:56:37 1998::dfyicjw@fklbymd.org::885650197-7-7')
('Sun', ' Jun  3 07:48:45 2007::wptuyjk@tsngnle.edu::1180828125-7-7')
('Mon', ' Nov 19 00:34:41 2001::ocjlb@nusyk.net::1006101281-5-5')
('Sat', ' Dec  1 21:01:23 1973::bvhx@lmir.net::123598883-4-4')
('Sun', ' Dec 16 17:42:51 1979::rpgs@hppau.org::314185371-4-5')
('Mon', ' Jul 21 23:46:13 1986::fnsro@nmbcwdmie.org::522344773-5-9')

---------------match_time_slot--------------------

('25', '2004', 'agmljfx@qvxgjqtkiwnl.org::1090704443-7-12')
('20', '2005', 'aguqfd@hnrcaged.com::1108887034-6-8')
('23', '2003', 'cysfu@fnzdo.com::1045947057-5-5')

---------------match_name--------------------

('qvnc', 'ygeowwaf', 'com')
('yene', 'rtewqvvyfe', 'edu')
('wlyi', 'coagmnososzy', 'edu')
('zsgok', 'jkpiplcm', 'edu')
('paok', 'anpekysphicu', 'com')
('bodqoe', 'iydohek', 'org')
('fgiy', 'oppcjnafx', 'gov')
('agmljfx', 'qvxgjqtkiwnl', 'org')
('tctz', 'bcikib', 'gov')
('qqlfkf', 'isslbh', 'com')
('cpiqwau', 'drbpfsfglip', 'edu')
('aguqfd', 'hnrcaged', 'com')
('kowyk', 'ruoackjavkpq', 'net')
('kqaol', 'mzewoas', 'edu')
('uofpdq', 'zdeidbobin', 'org')
('hodw', 'wfbw', 'org')
('azgagm', 'nfmguh', 'org')
('cysfu', 'fnzdo', 'com')
('qdhqw', 'fcdsvlmnhx', 'net')
('dfyicjw', 'fklbymd', 'org')
('wptuyjk', 'tsngnle', 'edu')
('ocjlb', 'nusyk', 'net')
('bvhx', 'lmir', 'net')
('rpgs', 'hppau', 'org')
('fnsro', 'nmbcwdmie', 'org')

Process finished with exit code 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值