python 基础之数据解析 -re

正则表达式,又称规则表达式,通常被用来检索、替换那些符合某个模式 / 规则的文本。
下面事正则表达式常见的使用场景:
(1)、检查字符串是否合法:

  • 验证用户名(a-z,0-9,不能全是数字,不能全是英文字母)
  • 验证邮箱格式(例如:xxx@qq.com)
  • 验证电话号码(11 位数字)
  • 验证身份证(18 位)
  • 验证 QQ 号码格式(5-12 纯数字,第一位不能为 0)

(2)提取字符串中的信息

  • 提取一条短信中的数字
  • 提取文件名的后缀
  • 采集器(网络爬虫)

(3)替换字符串

  • 替换字符串中的非法字符串
  • 对电话号码进行屏蔽
  • 替换占位符“hello {{name}}"hello 小红(模板框架)

(4)分割字符串

  • 将一个字符串按照指定的规则进行分割
  1. 元字符

使用元字符匹配单个字符
. 匹配任意 1 个字符(除了 \n)
[] 匹配 [] 中列举的字符
\d 匹配数字,即 0-9

import re
res = re.findall(“\d”, “0123456789@qq.com”)
print(res)

运行结果:

[‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’]

\D 匹配非数字,既不是数字
\s 匹配空白,即空格、tab 键、换行
\S 匹配非空白(数字、英文字符、特殊符号)
\w 匹配单词字符,即 a-z、A-Z、0-9、_
\W 匹配非单词字符
* 匹配前一个字符出现 0 次或者无数次,即可有可无
+ 匹配前一个字符出现 1 次或无限次,即至少一次

+* 匹配多个字符

import re
res = re.findall(“\d+”, “0123456789@qq.com”)
print(res)

运行结果:

[‘0123456789’]

\d 与 [](字符集)

有可能会出现这样的情况:字符集 [0123abc] 中只能匹配一个出现在字符集里面的值
\d 代表 0-9 的所有数字,[0123456789] 与 \d 等效

import re
res = re.findall(“[0123456789]+”, ‘0123456789@qq.com’)
print(res)

运行结果:

[‘0123456789’]

使用.* 匹配多个字符

匹配 Hello 与 Demo 之间的内容

impport re
str = “Hello World, this is a complete demo.”
res = re.findall(“Hello.*demo”, str)
print(res)

  1. 数量词

使用数量词匹配多个字符
{m} 匹配前一个字符出现 m 词
{m,n} 匹配前一个字符出现从 m 到 n 次

import re
res = re.findall(“[0-9]{10}”,“0123456789@qq.com
print(res)

  1. 精确匹配与泛匹配

精确匹配:就是匹配括号里面的东西

import re
content = “Hello 123World, this is a complete demo.”
res = re.find(‘Hello (\d+).*demo’, content)
print(res)

泛匹配:是指匹配所有的东西

import re
content = “Hello 123World, this is a complete demo.”
res = re.find(‘Hello.*demo’, content)
print(res)

  1. 贪婪匹配与非贪婪匹配

Python 里数量词默认是贪婪的(在少数语言里也可能是默认非贪婪),总是尝试匹配尽可能多的字符;
非贪婪相反,总是尝试匹配尽可能少的字符
而在贪婪匹配后加上? 就变成了非贪婪匹配

import re
content = “How are you feeling today? If your head’s in a spin because you’ve had a bad night’s sleep, or you’re hungover, or maybe you’ve just got too much to do, the chances are you’re not in the best of moods. If everything seems like an effort and you have a negative attitude, the last thing you want to be told it to ‘be positive!’ But having this mindset could have more benefits than you think.”
res = re.findall(‘(y.*u)’, content)
print(res)
result = re.findall(‘(y.*?u)’, content)
print(result)

运行结果:

[“you feeling today? If your head’s in a spin because you’ve had a bad night’s sleep, or you’re hungover, or maybe you’ve just got too much to do, the chances are you’re not in the best of moods. If everything seems like an effort and you have a negative attitude, the last thing you want to be told it to ‘be positive!’ But having this mindset could have more benefits than you”]
[‘you’, ‘y? If you’, ‘you’, ‘you’, ‘ybe you’, ‘you’, ‘ything seems like an effort and you’, ‘you’, ‘you’]

  1. 常见的 re 模块

正则表达式模块中有一些函数可以很方便的对字符串进行操作。

  • re.match

match() 用于查找字符串的头部(也可以指定起始位置),是一次性匹配,只要找到了一个匹配的结果就返回,而不是查找全部的匹配结果。使用形式为:

match(pattern, string[, flag])

pattern 是正则表达式规则字符串, string 是待匹配的字符,flag 是可选参数
当匹配成功时,返回一个 Match 对象,如果没有匹配上,则返回 None

  • re.search

search() 用于查找字符串的任何位置,也是一次性匹配,只要找到了一个匹配的结果就返回,而不是查找全部的匹配结果。使用形式为:

search(pattern, string[, flag])

pattern 是正则表达式规则字符串, string 是待匹配的字符,flag 是可选参数
当匹配成功时,返回一个 Match 对象,如果没有匹配上,则返回 None

  • re.split

split() 按照能够匹配的子串的将字符串分割后返回的列表,它的使用形式如下:

spit(pattern, string[, maxsplit, flags])

maxsplit 是用于指定最大分割数,不指定将全部分割

  • re.sub

sub() 用于替换,,使用形式如下:

sub(pattern, repl, string[, count, flag])

第一个参数为对应的正则表达式,第二个参数为要替换的字符串,第三个为源字符串,第四个参数为可选项,代表最多替换的次数,如果忽略不写,则会将符合模式的结果全部替换