前言: re模块是爬虫的基础,文章主要从符号和用法来介绍它的基础用法
各个符号及用法如下:
符号 | 含义 | |
---|---|---|
. | 表示匹配除了换行符外的任何字符 | |
\ | 转义字符 | |
[] | 字符集,匹配所包含的任意一个字符脱字符 ^ 如果出现在首位则表示匹配不包含其中的任意字符 | 、 |
预定义字符(可以写在[‘’‘’]里面) | ||
\d | 匹配一个数字 | |
\w | 单子字符,字母或者数字 | |
\s | 空白字符 | |
数量词:用在字符或者(''''')后面 | ||
* | 匹配前面的子表达式零次或多次,等价于 {0,} | |
+ | 匹配前面的子表达式一次或多次,等价于 {1,} | |
? | 匹配前面的子表达式零次或一次,等价于 {0,1} | |
{m,n} | M 和 N 均为非负整数,其中 M <= N,表示前边的 RE 匹配 M ~ N 次 | |
边界匹配 | ||
^ | 1. (脱字符)匹配输入字符串的开始位置 2. 如果设置了 re.MULTILINE 标志,^ 也匹配换行符之后的位置 | |
$ | 1. 匹配输入字符串的结束位置 2. 如果设置了 re.MULTILINE 标志,$ 也匹配换行符之前的位置 | |
\A | 匹配输入字符串的开始位置 | |
\Z | 匹配输入字符串的结束位置 | |
\b | 零宽断言,匹配一个单词边界,单词被定义为 Unidcode 的字母数字或下横线字符,举个例子:\bmiemei\b 会匹配字符串 "love meimei"、meimei." 或 "(meimei)" | |
\B | 零宽断言,匹配非单词边界,其实就是与 \b 相反,举个例子子:py\B 会匹配字符串 "python"、"py3" 或 "py2",但不会匹配 "py "、"py." 或 "py!" | |
逻辑分组 | ||
| | A | B,表示匹配正则表达式 A 或者 B,会优先匹配左边 | |
(''''') | 表示一个分组,匹配圆括号中的正则表达式,或者指定一个子组的开始和结束位置 注:子组的内容可以在匹配之后被 \数字 再次引用 |
各种方法代码如下: compile编译,返回一个对象 match匹配开头 两个参数(“re”,“str”),符合,不符合返回none,用.group()对象来返回匹配的字母 serach匹配第一个符合的组,在全文档查找, group()对象来查看返回结果 findall 匹配文档所有,返回一个列表,有分组的情况下只返回分组的结果 finditer返回对象,迭代出来,分组 split按正则分割,返回列表 sub 进行替换,subn返回替换结果以及替换次数 分组 贪婪和非贪婪 匹配中文字符"[\u4e00-\u9fa5]+"
'''
import re
# pattern = re.compile('\w\w\w')
# print(type(pattern))#<class '_sre.SRE_Pattern'>
# re01 = re.match("\w","hello world")
# print(re01)#<_sre.SRE_Match object; span=(0, 1), match='h'>
# print(re01.group())#h
#match
# str= "hello world,aaa"
# pattern = re.compile("(\w+) (\w+)")
# result = pattern.match(str)
# print(result.group())
# print(result.group(0))#0为默认返回完成匹配
# print(result.group(2))#返回第二个分组
#search
str= "hello world111111aaa"
pattern = re.compile("\d{2}")
result = pattern.search(str)
print(result.group())
print(result.group(0))#0为默认返回完成匹配
# print(result.group(2))#返回第二个分组
#findall
str= "hello world1111114aaa123rrrr4tdsfgdfgdsg"
pattern = re.compile("\d{2}")
result = pattern.findall(str)
print(result)
#有分组的情况
str= "hello world1111114aaa123rrrr4tdsfgdfgdsg"
pattern = re.compile("([a-z])[a-z]([a-z])")
result = pattern.findall(str)
print(result)
#finditer
str= "hello world1111114aaa123rrrr4tdsfgdfgdsg"
pattern = re.compile("([a-z])[a-z]([a-z])")
result = pattern.finditer(str)
for i in result:
print(i.group(2))#传入参数,返回匹配的分组
'''
split
'''
str= "hello world1111114aaa123rrrr4tdsfgdfgdsg"
pattern = re.compile("[a-w]+")
result = pattern.split(str)
print(result)
#sub
str= "hello world1111114aaa123rrrr4tdsfgdfgdsg"
pattern = re.compile("\d+")
result = pattern.sub("-",str)
print(result)
print(re.sub("\d","___",str))
#subn
str= "hello world1111114aaa123rrrr4tdsfgdfgdsg"
pattern = re.compile("\d+")
result = pattern.sub("-",str)
print(result)
print(re.subn("\d","___",str))
'''
分组
\2 \1分别表示第二组和第一组
'''
strs ="hello 123,world 321"
pattern = re.compile("(\w+) (\d+)" )
# for i in pattern.finditer(strs):
# print(i.group(0))
# print(i.group(1))
# print(i.group(2))
print(pattern.sub(r"\2 \1",strs))
print(pattern.sub(r"\2 *****\1",strs))
'''
贪婪匹配:在匹配成功的条件下尽量多的匹配
非贪婪匹配
'''
str = "aaa<p>hello </p>bbb<p>world</p>"
pattern = re.compile("<p>.*</p>")
result = pattern.findall(str)
print(result)
pattern = re.compile("<p>.*?</p>")
result = pattern.findall(str)
print(result)
'''
匹配中文字符
'''
str = "你好 hello 帅哥"
# pattern = re.compile("[\w]+")
pattern = re.compile("[\u4e00-\u9fa5]+")
result = pattern.findall(str)
print(result)