首页 > 代码库 > python day5--正则表达式
python day5--正则表达式
#----正则表达式
import re elink = ‘<a href="http://www.mamicode.com/(.*)">(.*)</a>‘ info = ‘<a href="http://www.baidu.com">baidu</a>‘ cinfo = re.findall(elink,info) print (cinfo)
import reprint(re.search (r‘^a‘,‘abc\neee‘))#预期结果 ^匹配字符开头# <_sre.SRE_Match object; span=(0, 1), match=‘a‘>data=http://www.mamicode.com/(re.match(‘^zhang‘,‘zhangyazhang‘))print(data)#<_sre.SRE_Match object; span=(0, 5), match=‘zhang‘>data.group() #获取到zhang这个值。
res=(re.match(‘^zhang\d‘,‘zhang123yazhang‘))print(res)#<_sre.SRE_Match object; span=(0, 6), match=‘zhang1‘>res=(re.match(‘^zhang\d+‘,‘zhang123yazhang‘))print(res)#<_sre.SRE_Match object; span=(0, 8), match=‘zhang123‘>res=(re.match(‘.+‘,‘zhang123yazhang‘))print(res)#匹配所有字符#<_sre.SRE_Match object; span=(0, 15), match=‘zhang123yazhang‘>data=http://www.mamicode.com/re.search(‘a.+d$‘,‘zhangabcd‘)print(data)#$匹配最后一个字符#<_sre.SRE_Match object; span=(2, 9), match=‘angabcd‘>print(re.findall("ab+","ab+cd+abb+bba"))#[‘ab‘, ‘abb‘]print(re.findall("ab*","cabcabb3bbac") )#--匹配*号前的字符0次或多次 (解释*号前是b,匹配b 0次或多次,a是前面必须有的,匹配b零次)#[‘ab‘, ‘abb‘, ‘a‘]print(re.findall(r‘\d+‘,‘a512b6‘))#[‘512‘, ‘6‘]print(re.findall(r‘\d+‘,‘one1two2three3four4‘))#[‘1‘, ‘2‘, ‘3‘, ‘4‘]
#-----sub的用法 --把content中的内容按link的模式替换成www.cnpythoner.comimport relink = ("\d+")content = "laowang-222haha"info = re.sub(link,‘www.cnpythoner.com‘,content)print (info)#laowang-www.cnpythoner.comhaha等价于print(re.sub("\d+",‘www.cnpythoner.com‘,"laowang-222haha"))
导入包的质是执行包下的_init_.py文件#bao\_init.pydef day_test(): print("in the day_test")day_test()#bao_test.py
import bao
import导入模块#--------main.pydef test(): print("in the test ")test()#--------module1.pyname = ‘alex‘def test2(): print("in the test2")test2()#---module.py执行#import main,module1# module1.test2()# print(module1.name)#---------另一种方式module.py执行# from main import test# from module1 import test2# test()# test2()
import re,timeprint(re.search("abc|ABC","ab1cABCBCD").group()) #ABCprint(re.search("[A-Za-z0-9]a","0aAB").group())#0aABprint(re.findall("[0-9]{1,3}","aa1x2a34567")) #[‘1‘, ‘2‘, ‘345‘, ‘67‘]print(re.search("(?P<province>[0-9]{4})(?P<city>[0-9]{2})(?P<birthday>[0-9]{4})","371419938106143242").groupdict("birthday")){‘province‘: ‘3714‘, ‘birthday‘: ‘9381‘, ‘city‘: ‘19‘}#‘\n‘是换行,‘\t‘是tab,‘\\‘是\print(re.search("(?P<id>[0-9]+)","abcd1234daf@34").group()) #1234print(re.search("(?P<id>[0-9]+)","abcd1234daf@34").groupdict(id)) #{‘id‘: ‘1234‘}print(re.split("[0-9]+","avb12fsd2ff3tt5D"))#[‘avb‘, ‘fsd‘, ‘ff‘, ‘tt‘, ‘D‘]print(re.sub("[0-9]+","|","ab1ee888iii0BBVV9$%"))#ab|ee|iii|BBVV|$%print(re.sub("[0-9]+","|","ab1ee888iii0BBVV9$%",count=2))#ab|ee|iii0BBVV9$%import reprint (re.search("\s+", "ab- \r\n ") )#<_sre.SRE_Match object; span=(3, 8), match=‘ \r\n ‘>
表示时间:1)时间戳 2)格式化的时间字符串 3)元组(struct_time)import timeres=time.time()print(res)#1472142901.0856912x=res/3600/24/365 #46.681345163802995print(x) #1970+46=2016print(time.localtime(323233211))x=323233211/3600/24/365print(x)#1970+10=1980 时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量y=time.localtime(323233211)print(y)print(y.tm_year) #y=1980y=time.localtime(323233211)print(y)#structprint(time.mktime(y))#元组转换成时间戳 323233211.0#print(time.asctime()) #等于print(time.asctime(time.localtime()))#Thu Aug 25 21:55:06 2016print(time.asctime()) #等于print(time.asctime(time.localtime()))#Thu Aug 25 21:55:06 2016data=time.strptime("2016/08/25","%Y/%m/%d") #将日期字符串 转成 struct时间对象格式print(data)res=time.mktime(data) #将struct时间对象转成时间戳print(res)
import datetime,timeprint(datetime.datetime.now()) #2016-08-25 23:42:44.481994print(datetime.date.fromtimestamp(time.time()) ) #2016-08-25print(datetime.datetime.now()+datetime.timedelta(hours=3)) #当前时间+3小时print(datetime.datetime.now()+datetime.timedelta(3)) #当前时间+3天print(datetime.datetime.now()+datetime.timedelta(minutes=3))#当前时间+3分钟c_time = datetime.datetime.now()print(c_time) #打印当前时间print(c_time.replace(minute=3,hour=2)) #时间替换
python day5--正则表达式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。