正则表达式注意事项

1.所谓组。您检查一次,你可以选择多个比赛结果,每个结果是指一组

eg.

import re
pattern = re.compile(r'<div(.*?

>)(.*?)</div>') match = pattern.match('<div class="test">Hello <div>wa haha<div>test xxx</div></div>world</div>') if match: print match.group(2) print match.group(1) print match.group(0) >>> ================================ RESTART ================================ >>> Hello <div>wa haha<div>test xxx class="test"> <div class="test">Hello <div>wa haha<div>test xxx</div> >>>

当中group(0)比較特殊,全部组的合集


2.查找一个文件里全部的http(url)地址

import re
import urllib2
f = open('url.txt','r')
string = ""
while 1:
    line = f.readline()
    if not line:break
    string += line

f.close()

urls = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', string)

3.正则表达式空格含有 fv如几个字符

版权声明:本文博客原创文章,博客,未经同意,不得转载。

原文地址:https://www.cnblogs.com/blfshiye/p/4736790.html