学习目的:

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

正式步骤

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方法不用限制匹配字符串的头部必须一致

Step5: re.compile

# -*-  coding:utf-8 -*-
"""
re.compile 将一个正则表达式串编译成正则对象,以便于复用该匹配模式--简言之就是代码复用
按我的理解就是下面例子中的pattern就是过滤条件
""" import re content = "I love python"
pattern = re.compile('I.*python',re.S)
result = re.match(pattern,content)
result1 = pattern.match(content)
print(result.group())
print(result1.group())

运行结果:

I love python
I love python

学习总结:


  正则表达式的应用需要多实践,在过滤爬取的数据时,应用较多

Python爬虫学习==>第九章:正则表达式基础的更多相关文章

  1. Python爬虫学习==>第二章:MongoDB环境配置

    学习目的: MongoDB的安装 正式步骤 (VMWare 虚拟机上无法安装这个MongoDB的自启动服务,如果你能办到,请多赐教) Step1:MongoDB的简介 MongoDB是一个基于分布式文 ...

  2. Python爬虫学习==>第一章:Python3+Pip环境配置

    前置操作 软件名:anaconda  版本:Anaconda3-5.0.1-Windows-x86_64清华镜像  下载链接:https://mirrors.tuna.tsinghua.edu.cn/ ...

  3. Python实战:Python爬虫学习教程,获取电影排行榜

    Python应用现在如火如荼,应用范围很广.因其效率高开发迅速的优势,快速进入编程语言排行榜前几名.本系列文章致力于可以全面系统的介绍Python语言开发知识和相关知识总结.希望大家能够快速入门并学习 ...

  4. 《Python爬虫学习系列教程》学习笔记

    http://cuiqingcai.com/1052.html 大家好哈,我呢最近在学习Python爬虫,感觉非常有意思,真的让生活可以方便很多.学习过程中我把一些学习的笔记总结下来,还记录了一些自己 ...

  5. [转]《Python爬虫学习系列教程》

    <Python爬虫学习系列教程>学习笔记 http://cuiqingcai.com/1052.html 大家好哈,我呢最近在学习Python爬虫,感觉非常有意思,真的让生活可以方便很多. ...

  6. python爬虫学习(1) —— 从urllib说起

    0. 前言 如果你从来没有接触过爬虫,刚开始的时候可能会有些许吃力 因为我不会从头到尾把所有知识点都说一遍,很多文章主要是记录我自己写的一些爬虫 所以建议先学习一下cuiqingcai大神的 Pyth ...

  7. Python爬虫学习:三、爬虫的基本操作流程

    本文是博主原创随笔,转载时请注明出处Maple2cat|Python爬虫学习:三.爬虫的基本操作与流程 一般我们使用Python爬虫都是希望实现一套完整的功能,如下: 1.爬虫目标数据.信息: 2.将 ...

  8. Python爬虫学习系列教程

    最近想学一下Python爬虫与检索相关的知识,在网上看到这个教程,觉得挺不错的,分享给大家. 来源:http://cuiqingcai.com/1052.html 一.Python入门 1. Pyth ...

  9. python爬虫学习 —— 总目录

    开篇 作为一个C党,接触python之后学习了爬虫. 和AC算法题的快感类似,从网络上爬取各种数据也很有意思. 准备写一系列文章,整理一下学习历程,也给后来者提供一点便利. 我是目录 听说你叫爬虫 - ...

随机推荐

  1. BZOJ1821 部落划分[最小生成树]

    方法一:套路性的,二分距离,然后把距离点对距离小于答案的边都联通起来,然后看集合数量超过k说明答案小,增大,否则减小. 方法二:贪心,类kruskal.n个点,k个连通块,则需要有效连接(同一个块内的 ...

  2. substring和substr的区别

    substring和subsrt都是获取指定位数 字符串的方法: 语法: substring(start,end)/substring(one); substr(start,end)/substr(o ...

  3. jsp根据某一行颜色来其他行的颜色

    jsp根据某一行颜色(单选框)来其他行的颜色 <c:choose> <c:when test="${v.color=='黑色' }"> <td sty ...

  4. robotframework 模拟滚动鼠标到底部

    Execute Javascript var ele = document.getElementsByClassName("right_main")[0];ele.scrollTo ...

  5. POJ 2142 - The Balance [ 扩展欧几里得 ]

    题意: 给定 a b n找到满足ax+by=n 的x,y 令|x|+|y|最小(等时令a|x|+b|y|最小) 分析: 算法一定是扩展欧几里得. 最小的时候一定是 x 是最小正值 或者 y 是最小正值 ...

  6. MessagePack Jackson 数据大小

    我们在使用 MessagePack 对 List 对象数据进行序列化的时候,发现序列化以后的二进制数组数据偏大的情况. 请注意,不是所有的 List 对象都会出现这种情况,这个根据你 List 对象中 ...

  7. CDOJ 1132 酱神赏花 dp+单调栈降低复杂度+滚动数组

    酱神赏花 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 262143/262143KB (Java/Others) Submit St ...

  8. 进程间通信之管道--pipe和fifo使用

    匿名管道pipe 函数原型: #include <unistd.h> int pipe(int fildes[2]); 参数说明 fildes是我们传入的数组,也是一个传出参数.filde ...

  9. Linux之防火墙iptables

    一.检查iptables服务状态 1.首先检查iptables服务的状态 [root@bogon ~]# service iptables status iptables: Firewall is n ...

  10. Linux 搭建Mysql主从节点复制

    Linux环境 Centos 6.6 64位 准备两台服务器,搭建一主一从,实现Mysql的读写分离和数据备份 主节点 192.168.43.10 leader 从节点 192.168.43.20 d ...