正则表达式常见函数(爬虫网页匹配)

本文介绍了正则表达式的使用,包括局部匹配的re.match()和re.search()函数,全局匹配的re.compile()配合findall()方法,以及正则表达式的替换操作。通过实例展示了如何使用正则表达式在爬虫中查找和提取网址。
摘要由CSDN通过智能技术生成

一、局部匹配(只匹配一个结果)

1.re.match()函数  :从源字符的串的开头进行匹配

2.re.search()函数 :在全文中进行检索并匹配

从例子中看区别:

import re
#string ="apythonhellomypythonisouorpythonend"
string = "hellomypythonispythonourpythonend"
pattern=".python."
result =re.match(pattern,string)
result2 = re.search(pattern,string)
print (result)
print (result2)

运行结果:


二、全局匹配(找出所有的匹配结果)

1)使用re.compile()对正则式进行预编译

2)编译后,使用findall()根据正则表达式从源字符串中奖匹配的结果全部找出

import re
#string ="apythonhellomypythonisouorpythonend"
string = "hellomypythonispythonourpythonend"
pattern=".python."
su = re.compile(patte
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值