代码要多敲 注释要清晰

 

#零宽断言
import re

#零宽断言
#(?=exp)零宽度正预测  先行断言
#先行断言的执行步骤从要匹配的字符的最右端找到第一个ing,
#再匹配前面的表达式,如果无法匹配则查找第二个ing
pattern = re.compile(r'[a-z]*(?=ing)')
s = pattern.findall('I love cooking and singing')
print(1,s)#1 ['cook', '', 'sing', '']

#(?<=exp)零宽度正回顾 后发断言
#匹配abc开头的字符串后面部分
pattern = re.compile(r'(?<=abc).*')
s = pattern.findall('abcdefgh')
print(2,s)#2 ['defgh']

#(?!exp) 零宽度负预测 先行断言
#匹配不连续包含字符串abc的单词 用serach或者match方法查看分析
pattern = re.compile(r'\b((?!abc)\w)+\b')
s = pattern.search('abc123,ade123')
print(3,s.group())# 用findall的方法3 ['3']是列表  用search的方法3 ade123是字符串

#(?<!exp)零宽度负回顾后发断言
#匹配前面不是小写字母的连续七位数字
pattern = re.compile(r'(?<![a-z])\d{7}')
s = pattern.findall('1sa14641482418sfda')
print(4,s)

#练习 下面有点复杂 可用其他方法拿取python 爬虫
# s = pattern.search('<div> python 爬虫</div>')

 

相关文章:

  • 2021-07-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-17
  • 2022-02-15
  • 2021-11-15
猜你喜欢
  • 2021-11-20
  • 2022-12-23
  • 2022-12-23
  • 2021-10-31
  • 2022-02-27
  • 2021-11-12
相关资源
相似解决方案