正则表达式
1.简介
正则表达式是一个特殊的字符序列,利用事先定义好的一些特定字符以及它们的组合组成一个“规则”,检查一个字符串是否与这种规则匹配来实现对字符的过滤或匹配。
正则表达式是字符串处理的有力工具,但是并不是Python独有的,其他语言也有。
Python中,re模块提供了正则表达式操作所需要的功能。
大多数字母和字符一般都会和自身匹配。如果在字符串前面加了***r***,表示对字符串不进行转义。
有些字符比较特殊,它们和自身并不匹配,而是表明应和一些特殊的东西匹配,或者会影响重复次数。这些特殊的字符我们称之为元字符。
re模块中**findall()**方法以列表的形式返回所有能匹配的子串,如果没有找到匹配的,则返回空列表。
import re
s = r'abc'
print(re.findall(s, 'aabaaab')) # 无匹配
print(re.findall(s, 'aabcaabc')) # 匹配两项
print("\nhi\n") # 转义
print(r"\nhi\n") # 加r无转义
'''
[]
['abc', 'abc']
hi
\nhi\n
'''
2.正则表达式元字符
一 “.”:表示除换行符以外的任意字符
与“.”类似(但不相同)的一个符号是“\S”,表示不是空白符的任意字符。注意是大写字符S。
import re
s = 'hi,i am a student.my name is Hilton.'
print(re.findall(r'i', s)) # 匹配所有i
# ['i', 'i', 'i', 'i']
print(re.findall(r'.', s)) # 匹配除换行符以外的任意字符
# ['h', 'i', ',', 'i', ' ', 'a', 'm', ' ', 'a', ' ', 's', 't', 'u', 'd', 'e', 'n', 't', '.', 'm', 'y', ' ', 'n', 'a', 'm', 'e', ' ', 'i', 's', ' ', 'H', 'i', 'l', 't', 'o', 'n', '.']
print(re.findall(r'i.', s)) # 匹配i后面跟除换行符以外的任意字符的形式
# ['i,', 'i ', 'is', 'il']
print(re.findall(r'i\S', s)) # 匹配i后面跟不是空白符的任意字符的形式
# ['i,', 'is', 'il']
二 “[]”:指定字符集
常用来指定一个字符集,例如:[abc]、[a-z]、[0-9];
元字符在方括号中不起作用,例如:[akm$]和[m.]中元字符都不起作用;
方括号内的“^”表示补集,匹配不在区间范围内的字符,例如:[3]表示除3以外的字符。
import re
s = 'map mit mee mwt meqwt'
print(re.findall(r'me', s)) # 匹配me
# ['me', 'me']
print(re.findall(r'm[iw]t', s)) # 匹配m后跟i或者w再跟t形式
# ['mit', 'mwt']
print(re.findall(r'm[.]', s))
# []
s = '0x12x3x567x8xy'
print(re.findall(r'x[0123456789]x', s)) # 匹配以x数字x格式的
# ['x3x', 'x8x']
print(re.findall(r'x[0-9]x', s)) # [0-9]与[0123456789]等价
# ['x3x', 'x8x']
print(re.findall(r'x[^3]x', s)) # x后跟不为3的字符再跟x
# ['x8x']
三 “ ^ ”:匹配行首
匹配以^后面的字符开头的字符串
import re
s = 'hello hello world hello Mary hello John'
print(re.findall(r'hello', s)) # 匹配hello
# ['hello', 'hello', 'hello', 'hello']
print(re.findall(r'^hello', s))
# ['hello']
经过对比,只要是”^”
这个字符是在中括号”[]”
中被使用的话就是表示字符类的否定,如果不是的话就是表示限定开头。我这里说的是直接在”[]”
中使用,不包括嵌套使用。
其实也就是说”[]”
代表的是一个字符集,”^”
只有在字符集中才是反向字符集的意思。
四 “ $ " :匹配行尾
匹配以$之前的字符结束的字符串
import re
s = 'hello hello world hello Mary hello John'
print(re.findall(r'hello$', s)) # 匹配以hello结尾的
# []
s = 'hello hello world hello Mary hello'
print(re.findall(r'hello$', s)) # 匹配以hello结尾的hello
# ['hello']
s = 'map mit mee mwt meqmtm$'
print(re.findall(r'm[aiw]$', s)) # 匹配ma、mi、mw结尾的字符串
# []
print(re.findall(r'm[aiw$]', s)) # 匹配ma、mi、mw结尾的字符串 $在[]中作为普通字符
# ['ma', 'mi', 'mw', 'm$']
print(re.findall(r'm[aim$]$', s)) # 匹配ma、mi、mw、mt、m$结尾的字符串
# ['m$']
五 ‘’ \ ‘’:反斜杠后面可以加不同的字符以表示不同的特殊意义
\b匹配单词头或单词尾;
\B与\b相反,匹配非单词头或单词尾;
\d匹配任何十进制数;相当于[0-9];
\D与\d相反,匹配任何非数字字符,相当于[^0-9];
\s匹配任何空白字符,相当于[\t\n\r\f\v];
\S与\s相反,匹配任何非空白字符,相当于[^\t\n\r\f\v];
\w匹配任何字母、数字或下画线字符,相当于[a-zA-Z0-9_];
\W与\w相反,匹配任何非字母、数字和下画线字符,相当于[^a-zA-Z0-9_];
也可以用于取消所有的元字符: \\、\[。
这些特殊字符都可以包含在[]中。如:[\s,.]将匹配任何空白字符、","或"."。
import re
s = '0x12x3x567x8xy'
print(re.findall(r'[0-9]', s)) #
# ['0', '1', '2', '3', '5', '6', '7', '8']
print(re.findall(r'\d', s)) # 同上
# ['0', '1', '2', '3', '5', '6', '7', '8']
print(re.findall(r'[x\d]', s)) # 匹配字母“x”或数字
# ['0', 'x', '1', '2', 'x', '3', 'x', '5', '6', '7', 'x', '8', 'x']
正则表达式除了能够匹配不定长的字符集,还能指定正则表达式的一部分的重复次数,所涉及的元字符有“*”、“+”、“?”、“{}”。
6.“ * ”:匹配位于*之前的字符或子模式的0次或多次出现
import re
s = 'a ab abbbbb abbbbbxa'
print(re.findall(r'ab*', s))
# ['a', 'ab', 'abbbbb', 'abbbbb', 'a']
7.“+”:匹配位于+之前的字符或子模式的1次或多次出现
import re
s = 'a ab abbbbb abbbbbxa'
print(re.findall(r'ab+', s))
# ['ab', 'abbbbb', 'abbbbb']
8.“?”:匹配位于?之前的0个或1个字符
当“?”紧随其他限定符(*、+、{n}、{n,}、{n,m})之后时,匹配模式是“非贪心的”。“非贪心的”模式匹配搜索到尽可能短的字符串,而默认的“贪心的”模式匹配搜索到的、尽可能长的字符串。
import re
s='a ab abbbbb abbbbbxa'
print(re.findall(r'ab+', s))
#['ab', 'abbbbb', 'abbbbb']
print(re.findall(r'ab+?', s))
#['ab', 'ab', 'ab']