首页 > 编程语言 > 详细

python模块 - re模块使用示例

时间:2015-06-24 12:59:58      阅读:145      评论:0      收藏:0      [点我收藏+]

http://blog.csdn.net/pipisorry/article/details/46619179

re模块匹配规则见:http://blog.csdn.net/pipisorry/article/details/25909899

python正则表达式的例子


日志分析时,假设给定的字符串:
char str = “10.10.1.1 [2015/04/22 +0800] /ab/cd/?test0=123&test2=234 xxxx”; 要从中获取2015/04/22、/ab/cd/和234等值。

str = “10.10.1.1 [2015/04/22 +0800] /ab/cd/?test0=123&test2=234 xxxx”
print(re.findall(“\d{4}/\d{2}/\d{2}|/\w{2}/\w{2}|(?<=test2=)\d+”, str))

用urllib2、re、os 模块下载文件的脚本


!/usr/bin/env python
importurllib2
importre
importos
URL=’http://image.baidu.com/channel/wallpaper
read=urllib2.urlopen(URL).read()
pat =re.compile(r’src=\’#\’” //.+?.js”>’)
urls=re.findall(pat,read)
fori inurls:
url=i.replace(‘src=\’#\’” /code>,”).replace(‘”>’,”)
try:
iread=urllib2.urlopen(url).read()
name=os.path.basename(url)
with open(name,’wb’) as jsname:
jsname.write(iread)
except:

printurl,”url error”

from:http://blog.csdn.net/pipisorry/article/details/46619179

ref:


python模块 - re模块使用示例

原文:http://blog.csdn.net/pipisorry/article/details/46619179

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!