正则表达式

用来处理字符串 对字符串进行检索和替换

import re
x='hello\\nworld'
#在正则表达式中\ 表示四个\
m=re.search('\\\\',x) #match search

#第一个参数为正则规则
#第二个参数为要匹配的字符串

print(m)
# <re.Match object; span=(5, 6), match='\\'>

#match 从头开始匹配失败返回none;search 在整个字符串中匹配
#filliter findall fullmatch
# m1=re.match(r'hello','hello world good morning')
# print(m1) #<re.Match object; span=(0, 5), match='hello'>
# m2=re.match(r'world','hello world good morning')
# print(m2) # None
# m3=re.search(r'w','helloworld')
# print(m3)
# #finditer 返回的是一个可迭代对象
# from collections.abc import Iterable
# m4=re.finditer(r'x','axcdefgxjklmxpqrstuvwxyz')
# print(isinstance(m4,Iterable))
# for i in m4:
# print(i)
# m5=re.findall(r'x\d+','tttax54asdfx34fghjx23333vbnmx1qwerxt')
# print(m5)
# # ['x54', 'x34', 'x23333', 'x1']
# m6=re.fullmatch(r'hello','hello')
# # <re.Match object; span=(0, 5), match='hello'>
# print(m6) #找出所有字符串里面的找不全为None
# m7=re.fullmatch(r'r.*d','rasdfghjkzxcvbbnmd')
# print(m7)
# m=re.search('a.*0','qwaretur0huiop0')
# # .任意字符 *出现任意次数 贪婪模式
# print(m.pos,m.endpos)
# print(m.group())
# print(dir(m))
# print(m.span())
# m9=re.search(r'(9.*)(?P<xxx>0.*)(5.*7)','da9fi0riel5kfsda7ifsdaifrit')
# #(?P<name>表达式) 给分组起个名字
# print(m9.groups())
# print(m9.group(0))
# print(m9.group(1))
# print(m9.group(2))
# print(m9.group(3))
# print(m9.string)
# print(m9.lastindex) #组索引
# print(m9.lastgroup)
# print(m9.groupdict('xxx'))
# print(m9.start())
# print(m9.end())
# print(m9.expand('qqqq'))
# r=re.compile(r'5.*7')
# x=r.search('da9fi0riel5kfsda7ifsdaifrit')
# print(x) #<re.Match object; span=(10, 17), match='5kfsda7'>
# 正则修饰符
# .除了换行的任意字符
#re.S 让点匹配换行
m8=re.search(r'd.*w','asdfg\nghwkeb',re.S)
print(m8)
#re.I 忽略大小写
m7=re.search(r'x','weXdef',re.I)
print(m7)
#re.M 让$匹配到换行
#\w 以数字 字母 下划线 + 出现一次以上 $以指定内容结尾
m6=re.findall(r'\w+$','i am boy\n you are girl\n he is man',re.M)
print(m6)
#\d 表示0-9 \D表示非数字
#\s 表示空格 \S表示非空白字符 \W非单词字符
#[]匹配列举中的元素
# ?前一个字符出现一次或零次
#{m} 前一个字符出现m次
#{m,n} 前一个字符出现m到n次,若m省略出现0次到n次
# ^ 以指定字符串开头 $以指定字符串结尾 |匹配左右任意一个表达式
# ^表示在[]里面取反
# r'^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\d{8}$'
# print(re.search(r'\s','he llo'))
# print(re.search(r'\S','\t c'))
# print(re.search(r'\n','hello\n world')) #换行
# print(re.search(r'\t','hello\tworld')) #制表符
# #():表示一个分组
# #[a-zA-Z0-9] 匹配数字和字母
# m0=re.search(r'h(\d+)x','wh890xdef')
# print(m0.group(1)) #分组包括第零个和第一个
#替换 sub
# t='abc3def45kdjjafgfg89474gf/ra'
# print(re.sub(r'\d','x',t)) #abcxdefxxkdjjafgfgxxxxxgf/ra
#第一个参数为正则表达式
#第二个参数为新字符
#第三个参数替换的字符串
#贪婪模式后面加?可以让贪婪变非贪婪
# c=re.search(r'm.*a','qwemoagas;faaaeera')
# c1=re.search(r'm.*?a','qwemoagas;faaaeera')
# print(c)
# print(c1)
# <re.Match object; span=(3, 18), match='moagas;faaaeera'>
# <re.Match object; span=(3, 6), match='moa'>
# x1=re.match(r'aa(\d+)','aa2343dddd')
# print(x1.group(0)) #aa2343
# print(x1.group(1))
# x2=re.match(r'aa(\d{2,}?)','aa2343dddd')
# print(x2.group(0)) #把两个执行完后面的限制了
# print(x2.group(1))
# x3=re.match(r'aa(\d+)ddd','aa2343dddd')
# print(x3.group(0))
# print(x3.group(1))
# x4=re.match(r'aa(\d+?)ddd','aa2343dddd')
# print(x4.group(0)) #没有被限制
# print(x4.group(1))
# x5=re.match(r'aa(\d??)(.*)','aa2343dddd')
# print(x5.group(0)) #aa2343dddd
# print(x5.group(1)) #空
# print(x5.group(2)) #2343dddd

# import re
# msa = 'asddjelmivlJheihieheiavralhJ'
# pattern = re.compile('heihiehei')
# pattern.match(msa)

# qq = '3228486670'
# result = re.match(r'[1-3]\d{4,10}$',qq)
# print(result)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值