Python3 正则表达式
示例代码1:
#!/usr/bin/python3import re# 使用 match对象 match = re.match('yhyang','aayhyang正在学习正则表达式')print(match) # 最开始匹配不到返回none输出:Nonematch = re.search('yhyang','aayhyang正在学习正则表达式')print(match.group()) # 直到找到匹配的字符串输出:yhyang
示例代码2:
#!/usr/bin/python3import reimport res = """1234-1234-113133-1234-2123125-4567-3456yhyang@foxmail.comlilei@qq.comhmm.lee@google.comhttp://baidu.comhttps://github.comhttp://taobao.com"""target = '\d{4}' # \d 匹配数字 {4} 表示4个 代表找到4个数字target = '\d{4}-\d{4}-\d{3}'target = '\d{3}-\d{4}-\d{4}'target = '\d+-\d+-\d+' # \d匹配数字,+ 表示1个或多个,search 找到第一个就停下来了target = '[\d+-?]+' # ? 表示后面跟0个或多个 [] 表示一个字符集如:[a-z]查找小写字母target = '\w{5}@\w{2}.com' # \w 匹配字符match = re.search(target,s)if match: print('找到,',target,s) print(match.group())else: print('啥都没找到')输出:找到, \w{5}@\w{2}.com 1234-1234-113133-1234-2123125-4567-3456yhyang@foxmail.comlilei@qq.comhmm.lee@google.comhttp://baidu.comhttps://github.comhttp://taobao.comlilei@qq.com
分组() 括号分组| 管道符号匹配多个分组?选择出现0次或1次re.x 换行,注释findall有分组,返回元组列表无分组,返回字符串列表 split返回用正则分割后的列表 compile 函数
compile 函数用于编译正则表达式,生成一个正则表达式( Pattern )对象,供 match() 和 search() 这两个函数使用。
语法格式为:re.compile(pattern[, flags])<br/>
参数:
pattern : 一个字符串形式的正则表达式flags 可选,表示匹配模式,比如忽略大小写,多行模式等,具体参数为: re.I 忽略大小写 re.L 表示特殊字符集 \w, \W, \b, \B, \s, \S 依赖于当前环境 re.M 多行模式 re.S 即为' . '并且包括换行符在内的任意字符(' . '不包括换行符) re.U 表示特殊字符集 \w, \W, \b, \B, \d, \D, \s, \S 依赖于 Unicode 字符属性数据库 re.X 为了增加可读性,忽略空格和' # '后面的注释
示例代码1:
#!/usr/bin/python3import reregex = re.compile(r'yhyang') # r 表示raw 原始的regex.findall('asdfasdfasfyhyang,235346747,yhyang')
输出:
['yhyang', 'yhyang']
示例代码2:
#!/usr/bin/python3import reregex = re.compile(r'(yhyang)') # () 分割,分组regex.split('asdfasdfasfyhyang,235346747,yhyang')
输出:
['asdfasdfasf', 'yhyang', ',235346747,', 'yhyang', '']
#!/usr/bin/python3import reparten = ((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)# 2[0-4]\d 匹配 24x x为0-9# 25[0-5] 匹配 25x x为0-5# [01]?\d\d? [01]? 匹配0个或是一个0或1,\d? 表示0个或1个数字# \. 转义 . # {3} 前面的规则匹配3次# 注意命名 re开头re_ip = re.compile(r'((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)')match = re_ip.match('1.2.3.4')print(match)print(match.group())
输出:
<_sre.SRE_Match object; span=(0, 7), match='1.2.3.4'>
1.2.3.4
示例代码2:
#!/usr/bin/python3import rere_ip = re.compile(r''' ((2[0-4]\d|25[0-5]|[01]?\d\d?)\.) # IP表示一组数字包括后边的点 {3} # 表示3组数字 (2[0-4]\d|25[0-5]|[01]?\d\d?) # 最后一组数字 ''',re.X) # 正则可以换行,注释match = re_ip.match('192.168.3.4')print(match)print(match.group())
输出:
<_sre.SRE_Match object; span=(0, 11), match='192.168.3.4'>
192.168.3.4
示例代码1:
#!/usr/bin/python3import re# 只匹配双引号中的内容,包括双引号re_quote = re.compile(r'"(.*)"')text1 = 'Computer says "no"'find1 = re_quote.findall(text1)print(find1)text2 = 'Computer says "no" Phone says "yes"'find2 = re_quote.findall(text2)print(find2)
输出:
['no']
['no" Phone says "yes']
示例代码2:
#!/usr/bin/python3import rere_quote = re.compile(r'"(.*?)"')text1 = 'Computer says "no"'find1 = re_quote.findall(text1)print(find1)text2 = 'Computer says "no" Phone says "yes"'find2 = re_quote.findall(text2)print(find2)
输出:
['no']
['no', 'yes']
示例代码:
#!/usr/bin/python3import res = 'sadfasfasdfasdfyhyangdsafasf2352'ss = s.replace('yhyang','******') # 替换print(ss)regex = re.compile(r'yhyang') # 生成规则对象regex.sub('******',s) # 替换
输出:
sadfasfasdfasdf**dsafasf2352
'sadfasfasdfasdf**dsafasf2352'
示例代码1:
#!/usr/bin/python3import re# 通过分组替换字符串格式,mm/dd/yy -> yy-mm-dds = '替换日期格式:10/01/2018,12/25/2018're_date = re.compile(r'(\d+)/(\d+)/(\d+)') # 此处给(\d+)/(\d+)/(\d+)按/分为三组,标号为\1,\2,\3re_date.sub(r'\3-\1-\2',s) # 将上边的\1,\2,\3位置按要求重新排列
输出:
'替换日期格式:2018-10-01,2018-12-25'
示例代码2:
#!/usr/bin/python3import re# 替换字符串中间的多余空格s = ' yhyang 正则 python 好难学, 坚持一下吧, 没 几 个了's.strip() # 只去掉最前边的re_blank = re.compile(r'\s+') # \s 代表空格re_blank.sub('',s)
输出:
'yhyang正则python好难学,坚持一下吧,没几个了'
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。