python day5--正则表达式
#----正则表达式
import re
elink = '<a href="(.*)">(.*)</a>'
info = '<a href="http://www.baidu.com">baidu</a>'
cinfo = re.findall(elink,info)
print (cinfo)
import re
print(re.search (r'^a','abc\neee'))
#预期结果 ^匹配字符开头
# <_sre.SRE_Match object; span=(0, 1), match='a'>
data=(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=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.com
import re
link = ("\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.py
def day_test():
print("in the day_test")
day_test() #bao_test.py
import bao


import导入模块
#--------main.py
def test():
print("in the test ")
test()
#--------module1.py
name = '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,time
print(re.search("abc|ABC","ab1cABCBCD").group()) #ABC
print(re.search("[A-Za-z0-9]a","0aAB").group())#0aAB
print(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})","").groupdict("birthday"))
{'province': '', 'birthday': '', 'city': ''}
#'\n'是换行,'\t'是tab,'\\'是\
print(re.search("(?P<id>[0-9]+)","abcd1234daf@34").group()) #
print(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 re
print (re.search("\s+", "ab- \r\n ") )
#<_sre.SRE_Match object; span=(3, 8), match=' \r\n '>
表示时间:1)时间戳 2)格式化的时间字符串 3)元组(struct_time) import time
res=time.time()
print(res)#1472142901.0856912
x=res/3600/24/365 #46.681345163802995
print(x) #1970+46=2016
print(time.localtime(323233211))
x=323233211/3600/24/365
print(x)#1970+10=1980 时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量
y=time.localtime(323233211)
print(y)
print(y.tm_year) #y=1980 y=time.localtime(323233211)
print(y)#struct
print(time.mktime(y))#元组转换成时间戳 323233211.0
#print(time.asctime()) #等于print(time.asctime(time.localtime()))
#Thu Aug 25 21:55:06 2016 print(time.asctime()) #等于print(time.asctime(time.localtime()))
#Thu Aug 25 21:55:06 2016 data=time.strptime("2016/08/25","%Y/%m/%d") #将日期字符串 转成 struct时间对象格式
print(data)
res=time.mktime(data) #将struct时间对象转成时间戳
print(res)
import datetime,time
print(datetime.datetime.now()) #2016-08-25 23:42:44.481994
print(datetime.date.fromtimestamp(time.time()) ) #2016-08-25
print(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--正则表达式的更多相关文章
- [python] 常用正则表达式爬取网页信息及分析HTML标签总结【转】
[python] 常用正则表达式爬取网页信息及分析HTML标签总结 转http://blog.csdn.net/Eastmount/article/details/51082253 标签: pytho ...
- Python 进阶 - 正则表达式
1. 正则表达式基础 1.1. 简单介绍 正则表达式并不是Python的一部分.正则表达式是用于处理字符串的强大工具,拥有自己独特的语法以及一个独立的处理引擎,效率上可能不如str自带的方法,但功能十 ...
- python study - 正则表达式
第 7 章 正则表达式 7.1. 概览 7.2. 个案研究:街道地址 7.3. 个案研究:罗马字母 7.3.1. 校验千位数 7.3.2. 校验百位数 7.4. 使用 {n,m} 语法 7.4.1. ...
- python使用正则表达式文本替换
2D客户端编程从某种意义上来讲就是素材组织,所以,图片素材组织经常需要批量处理,python一定是最佳选择,不管是win/linux/mac都有一个简单的运行环境 举两个应用场景: 如果不是在某个文件 ...
- python的正则表达式 re
python的正则表达式 re 本模块提供了和Perl里的正则表达式类似的功能,不关是正则表达式本身还是被搜索的字符串,都可以是Unicode字符,这点不用担心,python会处理地和Ascii字符一 ...
- Python之正则表达式(re模块)
本节内容 re模块介绍 使用re模块的步骤 re模块简单应用示例 关于匹配对象的说明 说说正则表达式字符串前的r前缀 re模块综合应用实例 正则表达式(Regluar Expressions)又称规则 ...
- Python:正则表达式详解
正则表达式是一个很强大的字符串处理工具,几乎任何关于字符串的操作都可以使用正则表达式来完成,作为一个爬虫工作者,每天和字符串打交道,正则表达式更是不可或缺的技能,正则表达式的在不同的语言中使用方式可能 ...
- 【Python】正则表达式纯代码极简教程
<Python3正则表达式>文字版详细教程链接:https://www.cnblogs.com/leejack/p/9189796.html ''' 内容:Python3正则表达式 日期: ...
- 【Python】正则表达式简单教程
说明:本文主要是根据廖雪峰网站的正则表达式教程学习,并根据需要做了少许修改,此处记录下来以备后续查看. <Python正则表达式纯代码极简教程>链接:https://www.cnblogs ...
- 【转】Python之正则表达式(re模块)
[转]Python之正则表达式(re模块) 本节内容 re模块介绍 使用re模块的步骤 re模块简单应用示例 关于匹配对象的说明 说说正则表达式字符串前的r前缀 re模块综合应用实例 参考文档 提示: ...
随机推荐
- iOS -Swift 3.0 -Array(数组与可变数组相关属性及用法)
// // ViewController.swift // Swift-Array // // Created by luorende on 16/9/12. // Copyright © 2016年 ...
- JS函数(获得widn)
//随机数生成器Math.random() 日期时间函数(需要用变量调用):var b = new Date(); //获取当前时间b.getTime() //获取时间戳b.getFullYear() ...
- MQ基础
1. 什么时候用activeMQ 在大量场合,ActiveMQ和异步消息对系统架构有意味深长的影响.下面举一些例子: 1). 异构系统集成 2). 取代RPC 3). 应用间的解耦 4). 事件驱动架 ...
- openSuSE DNS SERVER CONFIG
system:openSuSE 12.3(much better and frendly than the 12.1 in network config)1,network config,attent ...
- java io读书笔记(4)字符数据
Number只是java程序中需要读出和写入的一种数据类型.很多java程序需要处理有一大堆的字符组成的text,因为计算机真正懂得的只有数字,因此,字符按照某种编码规则,和数字对应. 比如:在ASC ...
- G面经prepare: Sort String Based On Another
Given a sorting order string, sort the input string based on the given sorting order string. Ex sort ...
- Python学习总结12:sys模块
sys模块常用来处理Python运行时配置以及资源,从而可以与前当程序之外的系统环境交互. 1. 导入及函数查看 >>> import sys #导入sys模块 >>&g ...
- C#: 集合
摘自http://www.cnblogs.com/kissdodog/archive/2013/01/29/2882195.html 先来了解下集合的基本信息 1.BCL中集合类型分为泛型集合与非泛型 ...
- 经常遇到Please ensure that adb is correctly located at 'D:\java\sdk\platform-tools\adb.exe' and can be e
遇到问题描述: 运行android程序控制台输出 [2012-07-18 16:18:26 - ] The connection to adb is down, and a severe error ...
- bean中集合属性的配置
在实际的开发中,有的bean中会有集合属性,如下: package com.sevenhu.domain; import java.util.List; /** * Created by hu on ...