python--正则表达式-匹配详解+练习题

python--正则表达式-匹配详解+练习题,第1张

概述一、匹配详解#!/usr/bin/python3#-*-coding:utf-8-*-#@Date:2021/5/26#@Name:ZhouZongXin"""注解:在正则表达式匹配的路上,是一半观察一半书写匹配字符:.[...][^...]\d\D\w\W\s\S匹配重复:*+?{n}{m,n}匹配位置:^$\b\B请他:|()\"""

一、匹配详解

#!/usr/bin/python3# -*- Coding: utf-8 -*-# @Date  : 2021/5/26# @name  : ZhouZongXin"""注解:在正则表达式匹配的路上,是一半观察一半书写匹配字符:. [...] [^...] \d \D \w \W \s \S匹配重复: * + ? {n} {m,n}匹配位置: ^ $ \b \B请他: | () \"""import reprint(re.findall(r'\d+', "2020-06-30"))  # \d+获取数字print(re.findall(r'a', "wjadiadaijada"))print(re.findall(r'ad', "wjadiadaijada"))print(re.findall(r'你好', "你好,你好,上海,你好"))print(re.findall(r'com|cn', "www.baIDu.com/www.tm.cn"))  # |管道符代表匹配两个字符print(re.findall(r'张.丰', "张三丰,张四风,张五丰"))  # .点代表匹配任意一个字符,除了换行print(re.findall(r'[A-Z]', "How are you!"))  # [字符集]匹配字符集中的任意一个字符,A-Z区间内任意字符print(re.findall(r'[a-z]', "How are you!"))  # [字符集]匹配字符集中的任意一个字符,a-z区间内任意字符print(re.findall(r'[0-9]', "How are you!"))  # [字符集]匹配字符集中的任意一个字符,0-9区间内任意字符print(re.findall(r'[!A-Z0-9]', "How are you!"))  # [字符集]匹配字符集中的任意一个字符(!),A-Z0-9区间内任意字符print(re.findall(r'[^0-9]', "How are you! 1994 one"))  # ^上间号,匹配字符集[0-9]之外的任意一个字符print(re.findall(r'[^ ]', "How are you! 1994 one"))  # ^上间号,匹配字符集[ ](空格)之外的任意一个字符print(re.findall(r'^Jame', 'Jame,hello'))  # 单引号内+上间号,代表匹配开头位置print(re.findall(r'^Jame', 'hello,Jame'))  # 因为字符串不在开头位置,所以无法匹配print(re.findall(r'Jame$', 'hello,Jame'))  # 单引号内+$,代表匹配结尾位置print(re.findall(r'^Jame$', 'Jame'))  # 单引号内有^+$,代表绝对匹配,中间有任何字符串都无法匹配到print(re.findall(r'to*', "too~~~t!"))  # o* 是一个整体,o代表后面匹配出现0次或者多次print(re.findall(r'[a-z]*', "too~~~t!"))  # [a-z]* 是一个整体,代表后面匹配出现0次或者多次,可能会匹配到空字符串print(re.findall(r'to+', "too~~~t!"))  # o+ o最少出现1次或者多次print(re.findall(r'to?', "too~~~t"))  # o? 代表o前面的字符出现0次或1次print(re.findall(r'to{5}', "tooooo~~~t"))  # {n} 匹配出现重复指定次数print(re.findall(r'to{2,4}', "tooooo~~~too"))  # {m,n} 匹配从第m次开始,到n次print(re.findall(r'\d{1,5}', "MysqL: 3306, http: 80"))  # \d匹配任意数字字符,区间在1-5位数print(re.findall(r'\D{1,5}', "MysqL: 3306, http: 80"))  # \D匹配任意非数字字符,区间在1-5位数print(re.findall(r'\w+', "srever_prop = '8888', .你好!@#¥¥%……&*("))  # \w匹配普通字符(数字,字母,下划线,汉字)print(re.findall(r'\W+', "srever_prop = '8888', .你好!@#¥¥%……&*("))  # \W匹配非普通字符(除了数字,字母,下划线,汉字的其他字符)print(re.findall(r'\w+\s+\w+\s+\w+\W+', "hello  world   你好!"))  # \s匹配空字符(空格 \r \n \t \v \f)print(re.findall(r'\S+', "hello  world   你好!"))  # \S匹配非空字符(除了空格 \r \n \t \v \f的其他字符)print(re.findall(r'\b.+\B', "!H-007 HELLO (H5)"))  # \b 匹配单词边界指数字,字母,汉字,下划线与其他字符的交界位置(r是转义的含义)print(re.findall(r'\B.+\b', "H-007 HELLO (H5)"))  # \B 匹配单词非边界指数字,字母,汉字,下划线与其他字符的交界位置(r是转义的含义)print(re.findall(r'\b[0-9]{5}\b', "1234567   77568,78787"))print(re.findall(r'$\d+', "日薪资:0"))  # \ 转移字符print(re.findall(r'(ab)', "abababab"))  # (ab)内部分组,匹配重复字符# 分组print(re.search(r'(ab)+', "abababab").group())print(re.search(r'(王|李)\w{1,3}', "王宝强, 李时珍").group())# 捕获组print(re.search(r'(?P<tel>1\d{10})\D+', "13512345678是我的电话").group('tel'))  # ?P<tel> 只捕获tel组内的字符

二、练习

#!/usr/bin/python3# -*- Coding: utf-8 -*-# @Date  : 2021/5/27# @name  : ZhouZongXin""""""import rea = '月全食是比较难得一见的天象。寇文称,这次月全食是今年唯一的一次。上次月全食出现在2019年1月21日,我国看不到。再上一次发生在2018年7月28日,' \    '我国大部地区可以在凌晨看到。接下来的几次月全食我国都看不到。2022年会发生两次月全食,其中5月16日的月全食,我国完全看不到;11月8日的月全食,' \    '我国大部分地区只能看到偏食。2025年又会出现两次月全食,发生在3月14日的月全食,我国不可见。出现在9月7日后半夜的月全食我国可以看到,而且观测条件很好。-1,3.5,-3.5'b = 'One needs 3 things to be truly happy living in the world: Some thing to do, some one to love, some thing to hope for. I lovE YOU'c = '13522568615,13927873121,15227873121,98786,889429822,889429857'd = '[email protected], [email protected], [email protected], adda3_3qd@qq.你好'e = '《爱你不是两三天》,《别走-2008》,《走吧-2020》,《今天,你吃了吗?》'# 1、在一个字符串文本中选出所有的正整数print(re.findall(r'[1-9][0-9]*', a))  # 首位不能为零# 2、匹配大学字母开头的单词print(re.findall(r'[A-Z][A-Za-z]*', b))  # 此时*比+号合适,因为I是单独的一个,如果用+那么不会匹配到I# 3、在一个字符串文本中选出所有的整数(包含负数)print(re.findall(r'-?\d\.?\d*', a))  # ?匹配一个或0个,因此区分有没有负号,在用转移字符\转移.的含义并且匹配0次或1次# 4、匹配出手机号print(re.findall(r'1[35789][0-9]{9}', c))  # 匹配手机号1开头,第二位为:35789区间,第三位0-9,后几位匹配0-9内重复数据9次# 5、匹配邮箱print(re.findall(r'[a-zA-Z0-9\w]+@[a-zA-Z0-9\w]+\.com', d))# 6、匹配所有书名print(re.findall(r'《.+?》', e))
总结

以上是内存溢出为你收集整理的python--正则表达式-匹配详解+练习题全部内容,希望文章能够帮你解决python--正则表达式-匹配详解+练习题所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1185622.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存