Python-正则表达式

本文详细介绍了Python中RE模块的功能与用法,包括findall、search、split、match、compile、sub和subn等函数,以及group、groups、groupdict和span等方法的使用。同时,还讲解了正则表达式的各种元字符和模式修正符号,如.、d、s、w和等,以及re.M、re.I、re.S和re.U等模式修正符号的应用。
import re
'''
函数:
findall()    #会将所有的匹配的内容输出
search()     #返回一个正则的对象
split()      #侵害内容
match()      #也是将一个符合内容的值给取出来
compile      #先将正则表达式给编译出来,然后再用这个编译后的表达式来直接匹配内容
sub()        #将匹配出来的内容给替换成一个相关的内容
subn()       #将符合正则的内容给替换掉,并且显示替换多少个位置
=====================
group()      #将match()与search()返回的匹配对象转成一个元组来取值
groups()
groupdict()
span()            #返回匹配元素起始位置


原子
.
\d \D
\s \S
\w \W
\b

元字符
[] ^ $ | ()
r 用法

模式修正符号 如果同时用两个模式修正符号的话,则要用"|" 符号来隔开
re.M    #多行操作
re.I    #忽略大小写
re.S    #让点匹配换行的操作
re.U    #根据unicode来匹配字符


'''

# findall()用法
# resulte=re.findall(".","onetwot\nonehree",re.S)    #re.s 这样的话. 就能匹配\n的内容
# print(resulte)



# r=re.compile("one")
# result=r.match("onetwoonethree")
# print(result)


# resulte =re.split(r"\d","one1tow2three")
# print(resulte)

# result =re.sub(r"\d","雷","one1two2three",1)
# print(result)


# result =re.subn(r"\d","雷","one1two2three")
# print(result)

# result = re.findall("^one",'onenienie \nONEtwothree',re.M|re.I)
# print(result)

# r = re.search("one\d","one1zhilei one2 tientionezhilei")
# print(r)

# 分组内容

# result=re.match("(?P<name>o(n))","ononeitneitneitneit")
# print(result.group())       #获取所有获取的内容
# print(result.groups())      #将分组的内空给取出来
# print(result.groupdict())    #在分组中的正则内容的前面加一个?P(P大写)+<>里面放入key

# findall()方法的分组,只显示分组的内容
# result=re.findall("o(ne)a(b)c","oneabc twothreef oneabc rnien")
# print(result)
# [('ne', 'b'), ('ne', 'b')]



# print(result.groups())

# # split()分组中的使用
# result=re.findall(r'\w+\/\w+',"one.two \ abcone  one/two")
# print(result)


# result=re.split(r"(\.)","one.two")
# print(result)

  

转载于:https://www.cnblogs.com/leigepython/p/6550532.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值