正则表达式 2

匹配开头结尾

字符功能
^匹配字符串开头
$匹配字符串结尾

其中re.match自带^,这里只演示一下$的用法

In [17]: re.match("12a$","12a").group()
Out[17]: '12a'

In [18]: re.match("12a$","12ab").group()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-18-6c5a080b2ad6> in <module>()
----> 1 re.match("12a$","12ab").group()

AttributeError: 'NoneType' object has no attribute 'group'
# 没有$符号不会报错
In [19]: re.match("12a","12ab").group()
Out[19]: '12a'

匹配分组

字符功能
|匹配左右任意一个表达式
(ab)将括号中字符作为一个分组
\num引用分组num匹配到的字符串
(?p<name>)分组起别名
(?p=name)引用别名为name分组匹配到的字符串
| 相当于“或”
In [22]: re.match("12a|bb","12a").group()
Out[22]: '12a'

In [23]: re.match("12a|bb","bb").group()
Out[23]: 'bb'
(ab) 的使用
In [34]: re.match("([^-]*)-(\d+)","111-1234").group()
Out[34]: '111-1234'
\num 的使用 第一个()分组后匹配出来的字符可以用\1来表示,
则第二个()分组后匹配出来的字符可以用\2表示,以此类推
In [41]: re.match(r"<(\w*)><(\w*)>.*</\2></\1>","<html><h1>123456</h1></html>" ).group()
Out[41]: '<html><h1>123456</h1></html>'

In [42]: re.match(r"<(\w*)><(\w*)>.*</\2></\1>","<html><h1>www.itcast.cn</h2></html>" ).group()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-42-b24647114061> in <module>()
----> 1 re.match(r"<(\w*)><(\w*)>.*</\2></\1>","<html><h1>www.itcast.cn</h2></html>" ).group()

AttributeError: 'NoneType' object has no attribute 'group'
(?P<name>) 和 (?P=name)的使用
之前()分组后匹配到的字符串可以用\1来表示,
(?P<name>) 为匹配到的字符串起了名字
(?P=name)可以用之前起的名字来进行提取之前匹配到的字符串

并且其中的P为大写

In [48]: re.match(r"<(?P<aa>\w*)><(?P<aaa>\w*)>.*</(?P=aaa)></(?P=aa)>", "<html><h1>153</h1></html>").group()
Out[48]: '<html><h1>153</h1></html>'

re模块的其他用法

表格中的用法都不是从头开始匹配的

方法功能
search不要求必须在开头只要有即可
findall匹配到全部适合的字符串
sub把匹配到的字符串进行替换
split根据匹配进行切割字符串
search 的用法
In [50]: re.search(r"\d+", "abcd 1516").group()
Out[50]: '1516'
findall 的用法, findall返回的就是一个列表,不需要在进行.group()
In [52]: re.findall(r"\d+", "python = 9999, c = 7890, c++ = 12345")
Out[52]: ['9999', '7890', '12345']
sub 的用法, 
格式: re.sub(r"正则表达式","替换的字符串","匹配字符串")
返回更改后的字符串
In [55]: re.sub(r"\d+", "78", "dadad 99")
Out[55]: 'dadad 78'
split 的用法
返回的是一个列表
In [56]: re.split(r":","aaa:33:bbb")
Out[56]: ['aaa', '33', 'bbb']
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rich_Z_b_f

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值