python进阶六 正则表达式
一 正则表达式概述1.1 为什么要学习正则表达式在实际开发过程中经常会有查找符合某些规则的字符串比如邮箱、图片地址、手机号码等。想匹配或者查找符合某些规则的字符串就可以使用正则表达式了。1.2 什么是正则表达式正则表达式(regular expression)描述了一种字符串匹配的模式1、比如检索一个串是否含有某种子串检索2、比如匹配的子串做替换替换3、比如从一个串中取出符合某个条件的子串提取模式一种特定的字符串模式这个模式是通过一些特殊的符号组成的。正则表达式并不是Python所特有的在Java、PHP、Go以及JavaScript等语言中都是支持正则表达式的。1.3 正则表达式的功能① 数据验证表单验证、如手机、邮箱、IP地址② 数据检索数据检索、数据抓取 爬虫功能③ 数据隐藏135****6235 王先生④ 数据过滤论坛敏感关键词过滤…二 re模块什么是re模块在Python中需要通过正则表达式对字符串进行匹配时可使用re模块re模块使用三步走第一步导入re模块importre第二步使用match方法进行匹配操作resultre.match(pattern正则表达式,string要匹配的字符串,flags0)#flags : 可选表示匹配模式比如忽略大小写多行模式等第三步如果数据匹配成功使用group方法来提取数据result.group()案例1defdm01_match():# “”“匹配字符: 从大字符串中, 按照规则, 匹配符合条件的子串”“# 1 导入re模块importre# 2 使用match方法进行匹配操作# 2-1 在大的字符串中, 按照规则:“任意1个字符”“it”“任意1个字符”, 提取符合要求的子串# 注意: 提取出来的子串一定要符合规则resultre.match(.it.,aitcast)# 2-2 从左到右的匹配(不能跳, 不能从中间匹配), 一个字符一个字符的匹配此时结果是没有找到符合规则的子串# result re.match(.it., iloveitcast)# 3 使用group方法来提取数据ifresult:inforesult.group()print(info)else:print(没有找到符合规则的子串)if__name____main__:dm01_match()输出结果aitc案例2defdm01_match():# 扫描字符返回第一个成功的匹配 def search(pattern, string, flags0) importre resultre.search(\d.*,city:1beijing2.shanghai)# \d.*: 数字开头,任意多个字符字符结尾# result re.search(.\d., cityp.1.beijing2.shanghai)ifresult:print(result.group())else:print(没有匹配到)passif__name____main__:dm01_match()结果1beijing2.shanghai案例3defdm01_match():importre sentence车主说:你的刹车片应该更换了啊,嘿嘿# 正则表达式: 去除多余字符pr呢|吧|哈|啊|啦|嘿|嘿嘿rre.compile(patternp)mystrr.sub(,sentence)print(mystr--,mystr)# 正则表达: 删除除了汉字数字字母和。.- 以外的字符# \u4e00-\u9fa5 是用来判断是不是中文的一个条件p[^。\.\-\u4e00-\u9fa5_a-zA-Z0-9]rre.compile(patternp)mystrr.sub(,sentence)print(mystr--,mystr)# 半角变为全角 sentence.replace(,, ) 逗号 感叹号 问号sentence你好.mystrsentence.replace(.,。)print(mystr--,mystr)if__name____main__:dm01_match()结果mystr--车主说:你的刹车片应该更换了,mystr--车主说你的刹车片应该更换了啊嘿嘿 mystr--你好。三 正则表达式编写3.1 使用re模块匹配单个字符代码功能.匹配任意1个字符除了\n[ ]匹配[ ]中列举的字符[^指定字符]匹配除了指定字符以外的所有字符\d匹配数字即0-9\D匹配非数字即不是数字\s匹配空白即 空格tab键\S匹配非空白\w匹配非特殊字符即a-z、A-Z、0-9、_、汉字\W匹配特殊字符即非字母、非数字、非汉字案例1defdm01_match():importre# 1 . 匹配任意1个字符除了\n# 匹配数据: 从左向右匹配一个字符接着一个字符的匹配resultre.match(itcast.,itcast2)# 获取数据ifresult:inforesult.group()print(info)else:print(没有匹配到)if__name____main__:dm01_match()结果itcast2案例2defdm01_match():importre# 2 [ ] 匹配[ ]中列举的字符# [a-z] [A-Z] [0-9] [a-zA-Z0-9]# 匹配数据resultre.match(itcast[123abc],itcast376)# 获取数据ifresult:inforesult.group()print(info)else:print(没有匹配到)if__name____main__:dm01_match()结果itcast3案例3defdm01_match():importre# 3 \d 匹配数字,即0-9 [0123456789] [0-9]# 匹配数据resultre.match(itcast\d,itcast5)# 获取数据ifresult:inforesult.group()print(info)else:print(没有匹配到)if__name____main__:dm01_match()结果itcast53.2 使用re模块匹配多个字符代码功能*匹配前一个字符出现0次或者无限次即可有可无匹配前一个字符出现1次或者无限次即至少有1次?匹配前一个字符出现1次或者0次即要么有1次要么没有{m}匹配前一个字符出现m次{m,n}匹配前一个字符出现从m到n次案例1defdm01_match():importre# 1 * 匹配前一个字符出现0次或者无限次即可有可无resultre.match(itcast1*,itcast111123333itcast)# 1出现0次或者多次# result re.match(itcast\d*, itcast23333itcast) # 数字出现0次或者多次# result re.match(itcast\d*itcast, itcast123333itcast) # 数字出现0次或者多次 itcast开始 itcast结束# result re.match(itcast\d*itcast, itcastitcast) # 数字出现0次或者多次 itcast开始 itcast结束# 获取数据ifresult:inforesult.group()print(info)else:print(没有匹配到)if__name____main__:dm01_match()结果itcast1111案例2defdm01_match():importre# 2 匹配前一个字符出现1次或者无限次即至少有1次# 数字必须出现1次或者多次resultre.match(itcast\ditcast,itcast11333444itcast)# 获取数据ifresult:inforesult.group()print(info)else:print(没有匹配到)if__name____main__:dm01_match()结果itcast11333444itcast案例3defdm01_match():importre# 3 ? 匹配前一个字符出现1次或者0次即要么有1次要么没有# 数字出现0次或者1次resultre.match(itcast\d?itcast,itcastitcast)# 获取数据ifresult:inforesult.group()print(info)else:print(没有匹配到)if__name____main__:dm01_match()itcastitcast案例4defdm01_match():importre# 4 {m} 匹配前一个字符出现m次resultre.match(itcast\d{2}itcast,itcast12itcast)# 获取数据ifresult:inforesult.group()print(info)else:print(没有匹配到)if__name____main__:dm01_match()结果itcast12itcast案例5defdm01_match():importre# 5 {m,n} 匹配前一个字符出现从m到n次# 注意1:{2, 5} 中括号里面,逗号之后不能加空格resultre.match(itcast\d{2,5}itcast,itcast111itcast)# 获取数据ifresult:inforesult.group()print(info)else:print(没有匹配到)if__name____main__:dm01_match()itcast111itcast3.3 使用re模块匹配指定字符串开头或者结尾代码功能^匹配字符串开头$匹配字符串结尾案例1defdm01_match():importre# 1-1 ^ 匹配字符串开头# 匹配数据: 匹配1个数字开头的子串resultre.match(^\ditcast,2itcast)# 1 匹配1个数字开头 itcast# result re.match(^\ditcast, 22itcast) # 2 匹配不上# 获取数据ifresult:inforesult.group()print(info)else:print(没有匹配到)if__name____main__:dm01_match()结果2itcast案例2defdm01_match():importre# 3 匹配以数字为开头以数字为结尾resultre.match(^\d.*\d$,11itcast22)# 获取数据ifresult:inforesult.group()print(info)else:print(没有匹配到)if__name____main__:dm01_match()结果11itcast223.4 使用re模块提取分组数据代码功能|匹配左右任意一个表达式(ab)将括号中字符作为一个分组\num引用分组num匹配到的字符串案例1defdm01_match():importre# 1 需求在列表中[apple, banana, orange, pear]匹配apple和pearfruit[apple,banana,orange,pear]# 获取字符串数据forvalueinfruit:resultre.match(apple|pear,value)# 判断匹配是否成功ifresult:inforesult.group()print(我想吃的水果:,value)else:print(f这个不是我想吃的水果{value})if__name____main__:dm01_match()结果我想吃的水果:apple 这个不是我想吃的水果banana 这个不是我想吃的水果orange 我想吃的水果:pear