内容字号:默认大号超大号

段落设置:段首缩进取消段首缩进

字体设置:切换到微软雅黑切换到宋体

Python爬虫学习==>第九章:正则表达式基础

2018-04-16 17:44 出处:清屏网 人气: 评论(0

学习目的:

正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特点字符、及这些特点字符组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑。

正式步骤

Step1:常用匹配模式

Step2:最常规的匹配

import re

testString = 'I have 4Learned the python years'
print(len(testString))
result = re.match('^I\s\w{4}\s\d\w{7}.*years$',testString)
print(result)
print(result.group()) #现实匹配结果
print(result.span()  #现实匹配区间

运行结果:

32
<_sre.SRE_Match object; span=(0, 32), match='I have 4Learned the python years'>
I have 4Learned the python years
(0, 32)

范匹配:

.*可以把除了匹配的开头和结尾都匹配

import re
testString = 'I have 4Learned the python years'
print(len(testString))
result = re.match('^I.*years$',testString)
print(result)
print(result.group())
print(result.span())

匹配目标:

设置起始端点后,用()来把需要匹配的目标括号起来

import re

testString = 'I have Learned the python years'
print(len(testString))
result = re.match('^I\s\w{4}\s(\w+)\s.*years$',testString)
print(result)
print(result.group(1))
print(result.span())

贪婪匹配:

import re
testString = 'I have 7777 Learned the python years'
print(len(testString))
result = re.match('^I.*(\d+).*years$',testString)
print(result)
print(result.group(1))
print(result.span())

运行结果:

36
<_sre.SRE_Match object; span=(0, 36), match='I have 7777 Learned the python years'>
7
(0, 36)

非贪婪匹配

import re
testString = 'I have 7777 Learned the python years'
print(len(testString))
result = re.match('^I.*?(\d+).*years$',testString)
print(result)
print(result.group(1))
print(result.span())

运行结果:

36
<_sre.SRE_Match object; span=(0, 36), match='I have 7777 Learned the python years'>
7777
(0, 36)

Step3:匹配模式

包含换行符:

import re
testString = '''I have 7777
Learned the python years'''
print(len(testString))
result = re.match('^I.*(\d+).*years$',testString,re.S)
print(result)
print(result.group(1))
print(result.span())

转义:

import re
content = "i have $5.00"
result = re.match('i have \$5\.00',content)
print(result.group())

Step4: re.search

功能:扫描整个字符串,返回第一个成功的匹配

import re
testString = '''I have 7777
Learned the python years'''
print(len(testString))
result = re.search('I.*(\d+).*years$',testString,re.S)
print(result)
print(result.group(1))
print(result.span())

总结:为了匹配方便,能用search就不用match,因为search方法不用限制匹配字符串的头部必须一致

学习总结:

正则表达式的应用需要多实践


分享给小伙伴们:
本文标签: Python爬虫

相关文章

发表评论愿您的每句评论,都能给大家的生活添色彩,带来共鸣,带来思索,带来快乐。

CopyRight © 2015-2016 QingPingShan.com , All Rights Reserved.

清屏网 版权所有 豫ICP备15026204号