第一个参数:规则
第二个参数:替换后的字符串
第三个参数:字符串
第四个参数:替换个数。默认为0,表示每个匹配项都替换
实例:将空白处替换成-
>>> test="Hi, nice to meet you where are you from?"
>>> re.sub(r'\s','-',test)
'Hi,-nice-to-meet-you-where-are-you-from?'
>>> re.sub(r'\s','-',test,5) #替换至第5个
'Hi,-nice-to-meet-you-where are you from?'
>>>
4.5、re.split
re.split 用于来分割字符串
12 >>> help(re.split)
split(pattern, string, maxsplit=0)
第一个参数:规则
第二个参数:字符串
第三个参数:最大分割字符串,默认为0,表示每个匹配项都分割
实例:分割所有的字符串
>>> test="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"