Python爬虫实践(七):正则表达式(2) re模块的使用
来源:互联网 发布:数据库服务器 编辑:程序博客网 时间:2024/05/16 09:30
前面说到re
模块,可用于正则表达式,匹配字符主要用到的函数有以下几种:
#以下为匹配所用函数re.match(pattern, string[, flags])re.search(pattern, string[, flags])re.split(pattern, string[, maxsplit])re.findall(pattern, string[, flags])re.finditer(pattern, string[, flags])re.sub(pattern, repl, string[, count])re.subn(pattern, repl, string[, count])
方法/属性——-作用
- match()
决定 RE 是否在字符串刚开始的位置匹配
- search()
扫描字符串,找到这个 RE 匹配的位置
- re.split()
按照能够匹配的子串将string分割后返回列表
import repattern = re.compile(r'\d+')print re.split(pattern,'one1two2three3four4')## 输出 # ['one', 'two', 'three', 'four', '']
-findall()
找到 RE 匹配的所有子串,并把它们作为一个列表返回
import repattern = re.compile(r'\d+')print re.findall(pattern,'one1two2three3four4')### 输出 #### ['1', '2', '3', '4']
-finditer()
找到 RE 匹配的所有子串,并把它们作为一个迭代器返回
import repattern = re.compile(r'\d+')for m in re.finditer(pattern,'one1two2three3four4'): print m.group(),### 输出 #### 1 2 3 4
-re.sub(pattern, repl, string[, count])
使用repl替换string中每一个匹配的子串后返回替换后的字符串。
import repattern = re.compile(r'(\w+) (\w+)')s = 'i say, hello world!'print re.sub(pattern,r'\2 \1', s)def func(m): return m.group(1).title() + ' ' + m.group(2).title()print re.sub(pattern,func, s)### output #### say i, world hello!# I Say, Hello World!
- 当
repl
是一个字符串时,可以使用\id
或\g
、\g
引用分组,但不能使用编号0。 - 当
repl
是一个方法时,这个方法应当只接受一个参数(Match对象),并返回一个字符串用于替换(返回的字符串中不能再引用分组)。 count
用于指定最多替换次数,不指定时全部替换。
-re.subn()
返回 (sub(repl, string[, count]) 的替换次数)。
import repattern = re.compile(r'(\w+) (\w+)')s = 'i say, hello world!'print re.subn(pattern,r'\2 \1', s)def func(m): return m.group(1).title() + ' ' + m.group(2).title()print re.subn(pattern,func, s)### output #### ('say i, world hello!', 2)# ('I Say, Hello World!', 2)
pattern说明
以上这些函数中所用到的 pattern = re.compile(string[,flag])
而以上的flags
参数是字符的匹配模式,如果要同时应用多个匹配模式(规则),则可以使用按位或运算符 |
. flags
可选值:
- re.I
(IGNORECASE)
: 忽略大小写(括号内是完整写法,下同)
- re.M
(MULTILINE)
: 多行模式,改变’^’和’$’的行为(参见上图)
- re.S
(DOTALL)
: 点任意匹配模式,改变’.’的行为
- re.L
(LOCALE)
: 使预定字符类 \w \W \b \B \s \S
取决于当前区域设定
- re.U
(UNICODE)
: 使预定字符类 \w \W \b \B \s \S \d \D
取决于unicode定义的字符属性
- re.X
(VERBOSE)
: 详细模式。这个模式下正则表达式可以是多行,忽略空白字符,并可以加入注释。
注:如果在pattern
生成时已经指明了flags
,那么在匹配函数方法中就不需要传入这个参数了
最后,通过一例子来理解:
# -*- coding: utf-8 -*-# 导入re模块import re# 将正则表达式编译成Pattern对象,注意hello前面的r的意思是“原生字符串”pattern = re.compile(r'hello')# 使用re.match匹配文本,获得匹配结果,无法匹配时将返回Nonestrings=['hello','Hello','HELLOGIRL','hellooo','heyloo']i=1for str in strings: result=re.match(pattern,str) if result: print result.group() i+=1 else: print "第 %d 个匹配失败"%(i) i+=1
运行结果:
hello第 2 个匹配失败第 3 个匹配失败hello第 5 个匹配失败
加入re.X
如果将上面的pattern
改为 pattern = re.compile(r'hello*',re.I)
由上面的知识可知,re.I
即忽略大小写的意思,则运行结果:
helloHelloHELLOhello第 5 个匹配失败
如果pattern
改为: pattern = re.compile('hey*')
结果:
he第 2 个匹配失败第 3 个匹配失败hehey
- Python爬虫实践(七):正则表达式(2) re模块的使用
- Python爬虫实践(八):正则表达式re模块(2)
- Python爬虫正则表达式和re模块系列之七
- PYTHON RE模块 正则表达式的使用
- python 正则表达式 python re模块的使用
- python的re模块及正则表达式的使用
- python中正则表达式以及re模块的使用
- python正则表达式模块,re.sub的函数使用
- python正则表达式——re模块的使用
- python爬虫02 re模块与正则表达式
- python爬虫入门(1) 基础知识 ; 正则表达式 Re 模块
- Python 正则表达式 RE模块的使用方法
- Python的re模块学习---正则表达式
- 正则表达式和python的re模块
- re---Python的正则表达式模块
- Python 正则表达式 RE模块的使用方法
- Python re模块的正则表达式
- Python正则表达式re模块的使用方法
- CentOS 下安装 Node.js
- Android 实现点击震动效果
- 用脚本编译Mac app的环境安装-Maoyingyong
- ok,机房小感
- BZOJ3034: Heaven Cow与God Bull
- Python爬虫实践(七):正则表达式(2) re模块的使用
- Use adb over Wi-Fi
- docker 创建本地镜像服务器
- 解决 flume KafkaSink 启动后cpu占用100%的问题
- 在android studio中将某个module生成jar包,觉得还是直接用aar好
- 学习笔记——c++自定义class用于unordered
- SpringMVC-----Json视图返回@Entity对象报错
- 计算机的高级语言
- protobuf传输文件