普通字符和元字符构成的字符串,描述一类字符串规则。
导包:
import re
findall:把匹配上的内容全都添加到列表中。
match:从左往右匹配,匹配到后,返回结果;若匹配不上,则返回None。
import re
str1 = 'abcdab'
path1 = 'ab'
print(re.findall(path1, str1))
restr = re.match(path1, str1)
print(restr)
print(restr.group())
# 结果:['ab','ab']
# <re.Match object; span=(0, 2), match='ab'>
# ab
元字符
从左往右匹配单个字符。
字符集:匹配单个字符,使用中括号来表示,表示字符的范围
print(re.match('[abcd]', 'abc')) # <re.Match object; span=(0, 1), match='a'>
print(re.match('[abcd]', 'zabc')) # None
print(re.findall('[abcd]', 'zabc')) # ['a', 'b', 'c']
. 表示任意一个字符,除了\n。
print(re.match('.', 'web')) # <re.Match object; span=(0, 1), match='w'>
print(re.match('.', 'eweb')) # <re.Match object; span=(0, 1), match='e'>
print(re.match('.', '@web')) # <re.Match object; span=(0, 1), match='@'>
print(re.match('...', 'web')) # <re.Match object; span=(0, 3), match='web'>
print(re.match('.', '\nweb')) # None
print(re.match('.', '''''')) # None
\d 表示0-9之间的任意一个数字
\D 表示非数字
print(re.match('\d', '@web')) # None
print(re.match('\d', '2@web')) # <re.Match object; span=(0, 1), match='2'>
print(re.match('\d', '@4web')) # None 若在中间或结尾,则匹配不到
print(re.match('\D', '@4web')) # <re.Match object; span=(0, 1), match='@'>
print(re.match('\D', '2web')) # None
print(re.match('\D', '\nweb')) # <re.Match object; span=(0, 1), match='\n'>
\s 表示空白字符串 \n \t
\S 表示非空白
print(re.match('\s', ' ')) # <re.Match object; span=(0, 1), match=' '>
print(re.match('\s', '\nweb')) # <re.Match object; span=(0, 1), match='\n'>
print(re.match('\s', '\tweb')) # <re.Match object; span=(0, 1), match='\t'>
print(re.match('\S', '\t')) # None
print(re.match('\S', '\\')) # <re.Match object; span=(0, 1), match='\\'>
\w 英文字母、数字、下划线
\W 与上面\w相反
print(re.match('\w', '\\')) # None
print(re.match('\w', '_\\')) # <re.Match object; span=(0, 1), match='_'>
print(re.match('\w', 'A\\')) # <re.Match object; span=(0, 1), match='A'>
print(re.match('\W', '\\')) # <re.Match object; span=(0, 1), match='\\'>
print(re.match('\W', 'A\\')) # None
-表示一个范围内
print(re.findall('[a-d]', 'zabc')) # ['a', 'b', 'c'] [a-d]等价于[abcd]
print(re.findall('[a-z]', 'zabc')) # ['z', 'a', 'b', 'c']
print(re.findall('[a-z]', 'A')) # [] [a-z]是小写的
print(re.findall('[A-Z]', 'A')) # ['A']
print(re.findall('[a-zA-Z]', 'Aabc')) # ['A', 'a', 'b', 'c'] [a-zA-Z]等价于[A-z]
print(re.findall('[A-z]', 'Aabc')) # ['A', 'a', 'b', 'c']
print(re.findall('[a-zA-Z]', 'Aabc')) # ['A', 'a', 'b', 'c']
print(re.findall('[A-z]', 'Aabc')) # ['A', 'a', 'b', 'c']
# 注意 不能是a-Z
print(re.findall('[0-9A-z]', '9Aabc')) # ['9', 'A', 'a', 'b', 'c']
^ 取反 间接号 [^a-e] 即取 a-e 之外。
print(re.match('[^a-e]','e')) # None
print(re.match('[^a-e]','z')) # <re.Match object; span=(0, 1), match='z'>
print(re.match('[^a-e]','12')) # <re.Match object; span=(0, 1), match='1'>
字符集的多字符匹配。
print(re.match('[0-9][0-9]', '12')) # <re.Match object; span=(0, 2), match='12'>
print(re.match('[0-9][0-9]', '1a')) # None
print(re.match('[0-9][a-z]', '1a')) # <re.Match object; span=(0, 2), match='1a'>
print(re.match('\d\w', '1a')) # <re.Match object; span=(0, 2), match='1a'>
print(re.match('\w\w', '1a')) # <re.Match object; span=(0, 2), match='1a'>
print(re.match('[\w\W][\w\W][\w\W][\w\W][\w\W][\w\W]', 'a张三@24')) # <re.Match object; span=(0, 6), match='a张三@24'>
print(re.findall('[\w\W]', 'a张三@24')) # ['a', '张', '三', '@', '2', '4']
数量规则
*匹配前一个字符的任意次数,包括0次。匹配到非数字的时候就会停止。
print(re.match('\d*', '12345678901')) # <re.Match object; span=(0, 11), match='12345678901'>
print(re.match('\d*', '1234a5678901')) # <re.Match object; span=(0, 4), match='1234'>
# 贪婪模式
print(re.match('\w*', '1234a5678901')) # <re.Match object; span=(0, 12), match='1234a5678901'>
+匹配前一个字符重复匹配1次以上 0次不行。
print(re.match('\d+', '12a345678901')) # <re.Match object; span=(0, 2), match='12'>
print(re.match('\d*', 'a1234')) # <re.Match object; span=(0, 0), match=''>
print(re.match('\d+', 'a1234')) # None
? 匹配前一个字符出现0次或者1次
print(re.match('\d?', '12a345678901')) # <re.Match object; span=(0, 1), match='1'>
print(re.match('\d?', '')) # <re.Match object; span=(0, 0), match=''>
次数匹配,就是出现多少次
{m} 匹配前m个字符
{m,} 至少匹配m次以上
{m,n} 匹配m次以上,n次以下
print(re.match('\d{5}', '12345678901')) # <re.Match object; span=(0, 5), match='12345'>
print(re.match('\d{5,}', '12345678901')) # <re.Match object; span=(0, 11), match='12345678901'>
print(re.match('\d{5,}', '1234')) # None
print(re.match('\d{3,6}','12345678901')) # <re.Match object; span=(0, 6), match='123456'>
print(re.match('\d{3,6}','12')) # None
边界处理
^ 开始,不是取反 取反在字符集里面
$ 结束
*eg:*电话号码:第一位 [1],第二位 [358],第三位 [3456789]。
print(re.match('^1[358][3-9]\d{8}$','13351798261')) # <re.Match object; span=(0, 11), match='13351798261'>
分组匹配
*eg:*匹配日期:01 02 03 04 05 06 07 08 09 10 11 12;或者:|;分组作用:()
t = '2022-10-13'
print(re.match('\d{4}-(0[1-9]|1[0-2])-([0-2]\d|[3][0-1])', t)) # <re.Match object; span=(0, 10), match='2022-10-13'>
group
t = '2022-10-13'
print(re.match('\d{4}-(0[1-9]|1[0-2])-([0-2]\d|[3][0-1])', t).group(0)) # 显示全部 2022-10-13
print(re.match('\d{4}-(0[1-9]|1[0-2])-([0-2]\d|[3][0-1])', t).group(1)) # 显示月份 10
print(re.match('\d{4}-(0[1-9]|1[0-2])-([0-2]\d|[3][0-1])', t).group(2)) # 显示天数 13
print(re.match('(\d{4})-(0[1-9]|1[0-2])-([0-2]\d|[3][0-1])', t).group(1)) # 显示年份 2022
print(re.match('(\d{4})-(0[1-9]|1[0-2])-([0-2]\d|[3][0-1])', t).groups()) # 把分组放到元组里面 ('2022', '10', '13')
r :规避/
\1 代表第一个分组中的内容
\2 代表第二个分组中的内容
context = '<title>hi</title>'
print(re.match(r'<title>([\w\W]*)</title>',context)) # <re.Match object; span=(0, 17), match='<title>hi</title>'>
print(re.match(r'<(\w+)>([\w\W]*)</\1>',context).group(1)) # title
# 分组取别名 ?P<别名>
print(re.match(r'<(?P<tag>\w+)>([\w\W]*)</(?P=tag)>',context).group()) # <title>hi</title>