python 正则表达式

(爬虫、Django之前讲)
需求:给你一个网站一个页面的源代码,需求把网址都选出来,返回列表。''。find()可以做但麻烦,如果需求复杂find不能胜任

正则:re regex,专业做字符串查找筛选,比''。find()强大的多。有自己专用的语法,优点:功能最为强大,缺点学习曲线陡峭。

场景:爬虫、网页解析、匹配、flask Django框架的路由就是基于正则的。

regex 三方包,功能比内置的re包更强大

前缀 r ,raw原始字符串,运行中不需要处理转义字符。

import re

find()      简单但功能有限不方便
html = r'

hello world

' start_index = html.find('

') eng_index = html.find('

') print(html[start_index:eng_index+1]) 1> 匹固定字符串一次 key = f'javapythonsldkfjlsjfpython' pattern1 = re.compile(r'python') matcher1 = re.search(pattern1,key) print(matcher1) print(matcher1[0]) compile(正则规则) 返回包含规则的匹配器对象 re.search(匹配器对象,带查找字符串) 2> 任意字符串 ,匹配任意字符 +修饰前面的匹配规则重复一次或多次 key2 = r'

hello world

' pattern2 = re.compile(r'

.+

') matcher2 = re.search(pattern2,key2) print(matcher2[0]) 3> 匹配 点 加号 。 转义\ 。 .匹配任意字符, +前面的字符一次或多次 key3 = r'[email protected]' p3 = re.compile(r'.+@qq\.com') # 判断用户输入的是否QQ邮箱。 .+不太准确 m3 = re.search(p3,key3) print(m3[0]) 4> *前面凡字符出现0或多次 key4 = r'http://www.baidu.com https://www.baidu.com' p4 = re.compile(r'https*://') m4 = re.search(p4,key4) matcher4 = p4.findall(key4) print(matcher4) 匹配器 findall(带匹配字符串) 返回列表 5> [aA]匹配一个字符 中括号里任意一个字符符合就算匹配到 key5 = r'SeLectSELECT' #sql大小写不敏感 p1 = r'[sS][Ee][Ll][Ee][Cc][Tt]' patter1 = re.compile(p1) print(patter1.findall(key)) 6> 排除 key6 = r'mat cat hat pat' p6 = re.compile(r'[^p]at') print(p6.findall(key6)) 7> 默认符合条件默认匹配可能多的字符 贪婪匹配 key7 = r'[email protected]' #需求街取出[email protected] p7 = re.compile(r'.+@.+\.') print(p7.findall(key7)) 8> 惰性匹配 符合任意多字符的情况下 字符最少的。 p8 = re.compile(r'.+@.+?\.') print(p8.findall(key7)) 9> 固定次数 key9 = r'saas and sas and saaas' p9 = re.compile(r'sa{1,2}s') print(p9.findall(key9)) 10> 匹配换行后的内容 key10 = r""" aaahelloaaa bbb world """ p10 = re.compile(r'hello.*?world',re.S) print(p10.findall(key10)) 11> 分组 (子正则式) key11 = r"""hello小明world""" p11 = re.compile(r'hello(.*?)world(.*?)') print(p11.findall(key11)) """ 正则规则总结: 云字符 --》说明 --》 """ 作业:课下百度re其他方法 re.match() re.findall() re.finditer()跟课上的re.comple re.search作比较

你可能感兴趣的:(python)