import re
text ='abc'# 从头查找'a'。若开头不是'a',则查找失败
result = re.match('a', text)# match方法返回的是一个对象,我们要用group函数把对象里的数据取出来print(result)# <re.Match object; span=(0, 1), match='a'>print(result.group())# a
import re
text ='abc'# 从头查找'b'
result = re.match('b', text)print(result)# None# 若继续提取数据,则会报错:print(result.group())# AttributeError: 'NoneType' object has no attribute 'group'
2.点("."):匹配任意的字符(除了’\n’)
我们仍然从text字符串中查找,只要字符串非空,就一定能匹配到一个字符。
import re
text ='abc'# 从头查找任意字符
result = re.match('.', text)print(result)# <re.Match object; span=(0, 1), match='a'>print(result.group())# a
这次我们将text稍作修改:text = ‘\nabc’
import re
text ='\nabc'# 从头查找任意字符
result = re.match('.', text)# 这时匹配的是'\n',但是'.'无法识别print(result)# Noneprint(result.group())# AttributeError: 'NoneType' object has no attribute 'group'
3."\d":匹配任意的数字(0-9)
将text稍作修改:text = “1ab”
import re
text ='1ab'# 从头查找数字字符
result = re.match('\d', text)print(result)# <re.Match object; span=(0, 1), match='1'>print(result.group())# 1
4."\D":匹配任意的非数字
我们的text仍然使用上一个例子中的:text = “1ab”
import re
text ='1ab'# 从头查找非数字字符
result = re.match('\D', text)print(result)# Noneprint(result.group())# AttributeError: 'NoneType' object has no attribute 'group'
import re
text ='\nabc'# 加强视觉效果print('~'*10)# 从头查找空白字符
result = re.match('\s', text)print(result.group())# 加强视觉效果print('~'*10)# ~~~~~~~~~~# # # ~~~~~~~~~~
6."\S":匹配非空白符
与"\D"的概念相似,\D匹配除了空白符以外的所有字符
import re
text ='abc'# 从头查找非空白字符
result = re.match('\S', text)print(result.group())# a
import re
text ='Hello World'
result = re.match('[a-zA-Z]+', text)print(result.group())# Hello
result2 = re.match('\d+', text)# 没有数字print(result2)# Noneprint(result2.group())# AttributeError: 'NoneType' object has no attribute 'group'
3."?":匹配0个或1个字符
"?"要在英文输入法下输入。其用法与前两个相似,我们直接上代码:
import re
text ='Hello World'
result = re.match('[a-zA-Z]?', text)print(result.group())# H(1个字符)
result2 = re.match('\d?', text)# 没有数字print(result2)# <re.Match object; span=(0, 0), match=''>print(result2.group())# 空(0个字符)
4."{m}":匹配m个字符
前几个都是正则表达式提前内置好的,如果我只想匹配2个或几个字符串的时候,就需要"{m}"来实现:
import re
text ='Hello World'# 匹配三个字符
result = re.match('[a-zA-Z]{3}', text)print(result.group())# Hel
import re
text ='abc'# 从字符串中查找'b'。只要字符串中存在,则查找成功
result = re.search('b', text)print(result)# <re.Match object; span=(1, 2), match='b'>print(result.group())# b
import re
text ="name(WangQiang),subject(Python)"
result = re.search('\(.+\)', text)# 左括号和右括号前面的"\"是转义字符print(result.group())# (WangQiang),subject(Python)
import re
text ="\cba c"# 将价格全部找出来
result = re.match(r'\\c', text)print(result.group())# \c
五.分组
通过分组,取不同部分的字符串
分组方法:括号"()"括起分组的字符串
概念理解起来有些吃力,我们直接上代码:
import re
text ="Apple price is $10, and banana price is $20."# 将字符串全部取出,并对价格进行分组
result = re.match(r'.+(\$\d+).+(\$\d+).+', text)print(result.group())# Apple price is $10, and banana price is $20.# 分组读取价格print(result.group(1))# $10print(result.group(2))# $20
分组的角标是存在0的,group(0)是将整体取出
import re
text ="Apple price is $10, and banana price is $20."# 将字符串全部取出,并对价格进行分组
result = re.match(r'.+(\$\d+).+(\$\d+).+', text)# 分组读取价格print(result.group(0))# Apple price is $10, and banana price is $20.
六.re模块常用函数
1.match/search(查找条件,被查找的字符串)
之前已经用过很多次,在此不做过多赘述
2.findall(查找条件,被查找的字符串):查找所有满足的字符串
import re
text ="Apple price is $10, and banana price is $20."# 将价格全部找出来
result = re.findall('\$\d+', text)print(result)# ['$10', '$20']
3.sub(被替换的字符串, 想替换的字符串, 被查找的字符串):根据规则替换其它字符串
sub方法比Python中的replace方法更强大,替换范围更广泛。替换之后返回替换好的字符串
例:将字符串中的空格替换成"-"
import re
text ="Apple price is $10 and banana price is $20"
result = re.sub(r' ','_', text)print(result)# Apple_price_is_$10_and_banana_price_is_$20
4.split(查找条件, 被查找的字符):根据规则切分字符串
将正则表达式中的查找条件当成分隔符
import re
text ="Apple price is $10, and banana price is $20."# 将价格作为分隔符
result = re.split(r'\$\d+', text)print(result)# ['Apple price is ', ', and banana price is ', '.']
import re
text ="Apple price is $10, and banana price is $20."# 使用compile进行编译
rule = re.compile('\$\d+')# 将价格作为分隔符
result = re.findall(rule, text)print(result)# ['$10', '$20']