re库的介绍
在爬取网站所有的内容后,我们不会一股脑的将数据都保存下来,通常我们要过滤,拿到我们想要的就好,其它就丢一旁。那么,我们就需要学会怎么使用正则表达式,通过它我们才能过滤出我们想要的内容。
什么是正则表达式
正则表达式,又称规则表达式,通常被用来检索、替换那些符合某个模式(规则)的文本。
正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符、及这些特定字符的组合,组成一个"规则字符串",这个"规则字符串"用来表达对字符串的一种过滤逻辑。
正则表达式的匹配规则
正则表达式的特殊序列
python中的re库
在 Python 中,我们可以使用内置的 re 模块来使用正则表达式。
有一点需要特别注意的是,正则表达式使用 对特殊字符进行转义,所以如果我们要使用原始字符串,只需加一个 r 前缀,示例:
r"shy-2\text\t\n\demo"
re库的使用
-
使用
compile()
函数将正则表达式的字符串形式编译为一个 Pattern 对象 -
通过
Pattern
对象提供的一系列方法对文本进行匹配查找,获得匹配结果,一个Match
对象。 -
最后使用
Match
对象提供的属性和方法获得信息,根据需要进行其他的操作
complie函数
compile 函数用于编译正则表达式,生成一个 Pattern 对象,它的一般使用形式如下:
re.compile(pattern[, flags])
把正则表达式的模式和标识转化成正则表达式对象,供 match() 和 search() 这两个函数使用。
re 所定义的 flag 包括:
re.I 忽略大小写
re.L 表示特殊字符集 \w, \W, \b, \B, \s, \S 依赖于当前环境
re.M 多行模式
re.S 即为’ . ’并且包括换行符在内的任意字符(’ . ’不包括换行符)
re.U 表示特殊字符集 \w, \W, \b, \B, \d, \D, \s, \S 依赖于 Unicode 字符属性数据库
re.X 为了增加可读性,忽略空格和’ # ’后面的注释
下面两种用法相同:
compiled_pattern = re.compile(pattern)
result = compiled_pattern.match(string)
result = re.match(pattern, string)
search函数
re.search(pattern, string[, flags])
在字符串中查找匹配正则表达式模式的位置,返回 MatchObject 的实例,如果没有找到匹配的位置,则返回 None。
对于已编译的正则表达式对象来说(re.RegexObject),有以下 search 的方法:
search (string[, pos[, endpos]])
若 regex 是已编译好的正则表达式对象,regex.search(string, 0, 50) 等同于 regex.search(string[:50], 0)
示例代码:
>>> pattern = re.compile("a")
>>> pattern.search("abcde") # Match at index 0
>>> pattern.search("abcde", 1) # No match;
match函数
re.match(pattern, string[, flags])
判断 pattern 是否在字符串开头位置匹配。对于 RegexObject,有:
match(string[, pos[, endpos]])
match() 函数只在字符串的开始位置尝试匹配正则表达式,也就是只报告从位置 0 开始的匹配情况,而 search() 函数是扫描整个字符串来查找匹配。如果想要搜索整个字符串来寻找匹配,应当用 search()。
split函数
re.split(pattern, string[, maxsplit=0, flags=0])
此功能很常用,可以将将字符串匹配正则表达式的部分割开并返回一个列表。对 RegexObject,有函数:
split(string[, maxsplit=0])
示例代码:
>>> re.split('\W+', 'test, test, test.')
['test', 'test', 'test', '']
>>> re.split('(\W+)', ' test, test, test.')
[' test ', ', ', ' test ', ', ', ' test ', '.', '']
>>> re.split('\W+', ' test, test, test.', 1)
[' test ', ' test, test.']
对于一个找不到匹配的字符串而言,split 不会对其作出分割,如:
>>> re.split('a*', 'hello world')
['hello world']
findall函数
re.findall(pattern, string[, flags])
在字符串中找到正则表达式所匹配的所有子串,并组成一个列表返回。同样 RegexObject 有:
findall(string[, pos[, endpos]])
示例如下:
#get all content enclosed with [], and return a list
>>> return_list = re.findall("(\[.*?\])",string)
finditer函数
re.finditer(pattern, string[, flags])
和 findall 类似,在字符串中找到正则表达式所匹配的所有子串,并组成一个迭代器返回。同样 RegexObject 有:
finditer(string[, pos[, endpos]])
sub函数
re.sub(pattern, repl, string[, count, flags])
在字符串 string 中找到匹配正则表达式 pattern 的所有子串,用另一个字符串 repl 进行替换。如果没有找到匹配 pattern 的串,则返回未被修改的 string。Repl 既可以是字符串也可以是一个函数。对于 RegexObject 有:
sub(repl, string[, count=0])
示例代码:
>>> p = re.compile( '(one|two|three)')
>>> p.sub( 'num', 'one word two words three words')
'num word num words num words'
同样可以用以下方法,并指定 count 为 1(只替换第一个):
>>> p.sub( 'num', ' one word two words three words', count=1)
' num word two words three words'
subn
re.subn(pattern, repl, string[, count, flags])
该函数的功能和 sub() 相同,但它还返回新的字符串以及替换的次数。同样 RegexObject 有:
subn(repl, string[, count=0])