设为首页 加入收藏

TOP

Python中re(正则表达式)模块详解(二)
2014-11-24 02:56:08 来源: 作者: 【 】 浏览:3
Tags:Python 正则 表达式 模块 详解
="Hi, nice to meet you where are you from "
>>> re.split(r"\s+",test)
['Hi,', 'nice', 'to', 'meet', 'you', 'where', 'are', 'you', 'from ']
>>> re.split(r"\s+",test,3) #分割前三个
['Hi,', 'nice', 'to', 'meet you where are you from ']
>>>


4.6、re.compile
re.compile 可以把正则表达式编译成一个正则对象
12 >>> help(re.compile)
compile(pattern, flags=0)


第一个参数:规则
第二个参数:标志位
实例:
>>> test="Hi, nice to meet you where are you from "
>>> k=re.compile(r'\w*o\w*') #匹配带o的字符串
>>> dir(k)
['__copy__', '__deepcopy__', 'findall', 'finditer', 'match', 'scanner', 'search', 'split', 'sub', 'subn']
>>> print k.findall(test) #显示所有包涵o的字符串
['to', 'you', 'you', 'from']
>>> print k.sub(lambda m: '[' + m.group(0) + ']',test) # 将字符串中含有o的单词用[]括起来
Hi, nice [to] meet [you] where are [you] [from]
>>>


五、用urllib2、re、os 模块下载文件的脚本
#!/usr/bin/env python
import urllib2
import re
import os
URL='http://image.baidu.com/channel/wallpaper'
read=urllib2.urlopen(URL).read()
pat = re.compile(r'src="http://.+ .js">')
urls=re.findall(pat,read)
for i in urls:
url= i.replace('src="','').replace('">','')
try:
iread=urllib2.urlopen(url).read()
name=os.path.basename(url)
with open(name,'wb') as jsname:
jsname.write(iread)
except:
print url,"url error"


推荐阅读:


首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Hadoop 用命令行编译URLCat 下一篇用Python备份MySQL数据库的脚本

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: