本文示例默认使用Python3作为实现语言,使用Python3的re模块或regex库。根据qbit的猜测:在Python3的Unicode字符集下,re模块的\s匹配\f\n\r\t\v加上全角和半角空格,共7个字符。RegularExpressionDocumentationRegularExpression30-minuteIntroductoryTutorial另一个揭开正则表达式神秘面纱的好入门教程。qbit觉得这篇文章对Multiline的解释很好。截图如下:提取双引号和它们之间的内容使用re.findalltext='''abc"def"ghi'''re.findall(r'"[^"]+"',text)#result['"def"']与re.search。>>>text='''abc"def"ghi'''>>>re.search(r'"([^"]+)"',text).group(0)'"def"'在双引号之间提取内容使用re.findall。text='''abc"def"ghi'''re.findall(r'"([^"]+)"',text)#results['def']withre.search.>>>text='''abc"def"ghi'''>>>re.search(r'"([^"]+)"',text).group(1)'def'环顾四周:(?<=pattern),(?=pattern)text='''abc"def"ghi'''re.findall(r'(?<=")[^"]+(?=")',text)#Result['def']查找以某些字符串开头的行#比如查找以+++,---,index开头的行#方法一,逐行匹配foriinlst:ifre.match(r"(---|\+\+\+|index).*",i):printi#方法二,一次性匹配re.findall(r'^(?:\+\+\+|---|index).*$',content,re.M)#方法2精简版re.findall(r'^(?:[-\+]{3}|index).*$',content,re.M)包含/不包含(参考:使用正则表达式排除特定字符串)文本内容>>>print(text)www.sina.com.cnwww.educ.orgwww.hao.ccwww.baidu.comwww.123.comsina.com.cneduc.orghao.ccbaidu.com123.com匹配以www开头的行>>>re.findall(r'^www.*$',text,re.M)['www.sina.com.cn','www.educ.org','www.hao.cc','www.baidu.com','www.123.com']匹配不以www>>>re.findall(r'^(?!www).*$',text,re.M)['','sina.com.cn','educ.org','hao.cc','baidu.com','123.com']匹配以cn结尾的行>>>re.findall(r'^.*?cn$',text,re.M)['www.sina.com.cn','sina.com.cn']匹配不以com结尾的行>>>re.findall(r'^.*?(?>>re.findall(r'^.*?com.*?$',text,re.M)['www.sina.com.cn','www.baidu.com','www.123.com','sina.com.cn','baidu.com','123.com']匹配不包含com>>>re.findall(r'^(?!.*com).*$',text,re.M)['www.educ.org','www.hao.cc','','educ.org','hao.cc']>>>re.findall(r'^(?:(?!com).)*?$',text,re.M)['www.educ.org','www.hao.cc','','educ.org','hao.cc']全部匹配,去掉部分,用分组得到第一级URL,即移除下几层#方法一>>>strr='http://www.baidu.com/abc/d.html'>>>re.findall(r'(http://.+?)/.*',strr)['http://www.baidu.com']#方法二>>>re.sub(r'(http://.+?)/.*',r'\1',strr)'http://www.baidu.com'两个有助于理解正则分组的例子#一个>>>strr='A/B/C'>>>re.sub(r'(.)/(.)/(.)',r'xx',strr)'xx'>>>re.sub(r'(.)/(.)/(.)',r'\1xx',strr)'Axx'>>>re.sub(r'(.)/(.)/(.)',r'\2xx',strr)'Bxx'>>>re.sub(r'(.)/(.)/(.)',r'\3xx',strr)'Cxx'#两个>>>text='AA,BB:222'>>>re.search(r'(.+),(.+):(\d+)',text).group(0)'AA,BB:222'>>>re.search(r'(.+),(.+):(\d+)',text).group(1)'AA'>>>re.search(r'(.+),(.+):(\d+)',text).group(2)'BB'>>>re.search(r'(.+),(.+):(\d+)',text).group(3)'222'提取包含hello字符串的div>>>content'
