標籤:python Regex re
http://blog.csdn.net/pipisorry/article/details/46619179
re模組匹配規則見:http://blog.csdn.net/pipisorry/article/details/25909899
pythonRegex的例子
日誌分析時,假設給定的字串:
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模組使用樣本