python之正则(二)

1.re的split,findall,finditer方法

正则对象的split方法
split(string[,maxsplit])
按照能够匹配的字符串将string分割后返回列表.maxsplit用于指定最大分割次数,不指定将全部分割.
import re
p=re.compile(r'\d+')
print(p.split('one1two2three3four4'))

 

正则对象的findall方法

findall(string[,pos[,endpos]])

搜索string,以列表形式返回全部能匹配的字符串;

import re

p=re.compile(r'\d+')
print(p.findall('one1two2three3four4'))


for i in p.finditer('one1two2three3four4'):
print(i.group())

2.re的match对象
match匹配对象:
Match对象是一次匹配的结果,包含了很多关于此次匹配的信息,可以使用Match提供的可读
属性或方法来获取这些信息.上面的过程中多次使用了match对象,调用了他的group()和
groups()等方法;


import re
prog = re.compile(r'(?P<tagname>abc)(\w*)(?P=tagname)')
result=prog.match('abcliuijde4543abc')
print(result.groups())
print(result.group('tagname'))
print(result.group(2))
print(result.groupdict())


#match对象的group返回一个元组,下标是以1开头;

 3.re的matche方法和search方法

match(string[,pos[,endpos]])
string:匹配使用的文本
pos:文本中正则表达式开始搜索的索引,及开始搜索string的下标;
endpos:文本中正则表达式结束搜索的索引.

如果不指定pos,默认是从开头开始匹配,如果匹配不到,直接返回None.
import re

pattern=re.compile(r'(hello w.*)(hello l.*)')
result=pattern.match(r'aahello world hello ling')
print(result)
result2=pattern.match(r'hello world hello ling')
print(result2.groups())

#search
result3=reg.search(b)
print(result3)

 

转载于:https://www.cnblogs.com/awenxianliao/p/7842598.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python爬虫中,正则表达式被广泛应用于文本数据的处理和匹配。正则表达式是一种使用单个字符串来描述、匹配和定位文本中某个特定模式的工具。它可以用来处理各种文本数据、数据采集和处理、爬虫和搜索等方面。在Python中,我们可以使用正则表达式来进行文本的匹配和提取。 在Python中,正则表达式的语法规则和匹配模式可以通过re模块来实现。re模块提供了一系列函数,包括match、search、findall和finditer等,用于对字符串进行匹配和搜索操作。其中,match函数用于从字符串的开头开始匹配,search函数用于在整个字符串中搜索匹配,findall函数用于找到所有匹配的结果,finditer函数用于返回一个迭代器,可以逐个返回匹配的结果。 在使用正则表达式时,我们可以通过定义特定的字符和字符组合来构建一个规则字符串,用来表达对字符串的一种过滤逻辑。例如,可以使用正则表达式来验证表单中的手机号、邮箱、身份证等信息。同时,正则表达式也可以用于爬虫中的数据采集和处理,通过匹配和提取特定的文本信息。 总之,正则表达式在Python爬虫中具有广泛的应用场景,可以帮助我们处理和匹配各种文本数据。通过使用re模块提供的函数和语法规则,我们可以灵活地应用正则表达式来满足不同的需求。 #### 引用[.reference_title] - *1* [python爬虫之正则详解](https://blog.csdn.net/naer_chongya/article/details/130737482)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [python爬虫之正则表达式(一)](https://blog.csdn.net/Claire_chen_jia/article/details/105999155)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Python爬虫之正则](https://blog.csdn.net/fanchen0218/article/details/130175356)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值