'''
正则表达式是一种工具:一种专门用来做字符串匹配的工具
能够在某些情况下让字符串的处理变得非常简单
'''
re_result
= r
'1[3-9]\d{9}'
result
= fullmatch
(re_result
, tel
)
print(result
)
'''
普通字符:普通字符在正则表达式中表示符号本身
'''
re_str
= r
'abc'
result
= fullmatch
(re_str
, 'abc')
print(result
)
re_str
= r
'.bc'
result
= fullmatch
(re_str
, 'xbc')
print(result
)
re_str
= r
'a..c'
result
= fullmatch
(re_str
, 'axxc')
print(result
)
print(fullmatch
(r
'a.b','a\nb',flags
=S
))
print(fullmatch
(r
'(?s)a.b','a\nb'))
print(fullmatch
(r
'a.b','a\nb',flags
=M
))
print(fullmatch
(r
'(?m)a.b','a\nb',flags
=S
))
print(fullmatch
(r
'ab','AB',flags
=I
))
print(fullmatch
(r
'(?i)ab','ABc'))
print(fullmatch
(r
'a.b','A\nb',flags
=I
| S
))
print(fullmatch
(r
'(?is)a.b','A\nb',flags
=I
| S
))
str1
= r
'\d\d\d'
result
= fullmatch
(str1
, '780')
print(result
)
str1
= r
'\d\dabc'
result
= fullmatch
(str1
, '12abc')
print(result
)
str1
= r
'\d\D\D\d'
result
= fullmatch
(str1
, '9a-0')
print(result
)
str1
= r
'abc\s123'
print(fullmatch
(str1
, 'abc 123'))
print(fullmatch
(str1
, 'abc\n123'))
print(fullmatch
(str1
, 'abc\t123'))
str1
= r
'abc\S\d\d\d'
print(fullmatch
(str1
, 'abc 123'))
print(fullmatch
(str1
, 'abc-633'))
'''
一个[]只能匹配一个字符
例:
[abc]---匹配一个字符是a或者是b或者是c
[\dabc]---匹配一个字符是数字或者a或者b或者c
[]中单独一个符号表示符号本身例如:[.]、[+]、[*]
[]中-在两个字符之间,必须是[小-大],如果是在两边就是-本身
[-ab]---匹配一个字符是-或者a或者b
[1-9]---匹配一个字符是1到9中的任意数字
[2-5abc]---匹配2到5中任意一个字符或者a或者b或者c
[a-zA-Z]---匹配任意字母
[\da-zA-Z_]---匹配字母、数字、下划线
[\u4e00-\u9fa5]---匹配任意一个中文
[^\u4e00-\u9fa5]---匹配任意一个非中文
'''
str1
= r
'[abc]123'
print(fullmatch
(str1
, 'a123'))
print(fullmatch
(str1
, 'b123'))
print(fullmatch
(str1
, 'c123'))
str1
= r
'[^abc]12'
print(fullmatch
(str1
, 'a12'))
print(fullmatch
(str1
, 'b12'))
print(fullmatch
(str1
, 'c12'))
str2
= r
'[a^b]123'
print(fullmatch
(str2
, 'a123'))
print(fullmatch
(str2
, '^123'))
print(fullmatch
(str2
, 'b123'))
'''
匹配符号要求一个符号必须对应一个字符,会影响字符串长度的描述
检测符号,不会匹配字符,也不会影响字符串长度
是在匹配成功的前提下对指定位置的字符进行检测
'''
'''
单词边界---所有能够区分出两个不同单词的符号,例如 :空白符号,标点符号,字符串开头和结尾
'''
str1
= r
'abc\b\s123'
print(fullmatch
(str1
, 'abc 123'))
str1
= r
'abc\B123'
print(fullmatch
(str1
, 'abc123'))
str1
= r
'abc^123'
print(fullmatch
(str1
, 'abc123'))
print(fullmatch
(str1
, 'abc^123'))
str2
= r
'^abc123'
print(fullmatch
(str1
, 'abc123'))
str1
= r
'^\d\d'
print(fullmatch
(str1
, '23'))
print(search
(str1
, '23dwad65wad'))
str1
= r
'\d\d$'
print(search
(str1
, 'da35awa12'))
'''
* 任意次数
a* --- 字符a出现0次或者多次
\d* ---任意数字出现任意次数
[字符集]*---字符集中的任意字符出现任意次数(每次都是任意一个)
'''
str1
= r
'\d*'
print(fullmatch
(str1
, '12'))
print(fullmatch
(str1
, ''))
print(fullmatch
(str1
, '1546'))
str1
= r
'a\d*b'
print(fullmatch
(str1
, 'ab'))
print(fullmatch
(str1
, 'a12345631321b'))
str1
= r
'1[a-z]*2'
print(fullmatch
(str1
, '1abcdwasdwa2'))
print(fullmatch
(str1
, '12'))
str1
= r
'[+]?[1-9]\d*'
print(fullmatch
(str1
, '132'))
'''
{N}---匹配N次
{M,N}---匹配M到N次
{M,}---匹配至少M次
{,N}---匹配最多N次
'''
str1
= r
'\d{3}'
print(fullmatch
(str1
, '111'))
name
= r
'[\da-zA-Z]{3,6}'
print(fullmatch
(name
, 'dad533'))
print(fullmatch
(r
'a{3,}123', 'aaaa123'))
print(fullmatch
(r
'a{,3}123', 'aaaa123'))
'''
匹配次数不确定的时候匹配模式有贪婪(默认)和非贪婪(在不确定次数后面加?)两种
贪婪:
在能匹配成功的前提下,匹配次数选最多的
非贪婪:
在能匹配成功的前提下,匹配次数选最少的
'''
str1
= 'a.+b'
print(search
(str1
, 'xxa111b2223b12234b'))
str1
= 'a.+?b'
print(search
(str1
, 'xxa111b2223b12234b'))
'''
(ab){M,N} ab整体重复M到N次
\M 重复前面的第M个分组中匹配到的内容
捕获:
re中findall在获取子串的时候,如果正则中有分组,只会获取分组中的内容
'''
str1
= r
'(\d{2}[a-zA-Z]{2}){3,5}'
print(fullmatch
(str1
, '01dw02dw64da15gy'))
str1
= r
'M(\d[a-z])N\1'
print(fullmatch
(str1
, 'M4aN4a'))
str1
=r
'a\d{2}b'
print(findall
(str1
,'ahaaba11ba1fba15b'))
str1
=r
'a(\d{2})b'
print(findall
(str1
,'ahaaba11ba1fba15b'))
'''
| 正则1|正则2|正则3
先判断正则1是否成立,成立则匹配成功,不成立继续往后判断,全部不成立那么就匹配失败
'''
str1
=r
'abc(\d{3}|[A-Z]{3})'
print(fullmatch
(str1
,'abc123'))
print(fullmatch
(str1
,'abcSSS'))
str1
=r
'abc\d{3}|abc[A-Z]{3}'
print(fullmatch
(str1
,'abc123'))
print(fullmatch
(str1
,'abcSSS'))
'''
在正则中有特殊意义的符号前加\,让这个符号在正则中的功能消失
'''
str1
=r
'\d{2}.\d{2}'
print(fullmatch
(str1
,'15+15'))
str1
=r
'\d{2}\.\d{2}'
print(fullmatch
(str1
,'15+15'))
str1
=r
'(\d{2})'
print(fullmatch
(str1
,'(15)'))
str1
=r
'\(\d{2}\)'
print(fullmatch
(str1
,'(15)'))
'''
. \d \D \s \S \w [] [^]
$ ^ \B \b
{} ? * + -
()
|
\
'''
转载请注明原文地址: https://lol.8miu.com/read-12921.html