从入门到入狱------正则


# 正则表达式 :只能用来处理字符串
'''
正则表达式是一种工具:一种专门用来做字符串匹配的工具
能够在某些情况下让字符串的处理变得非常简单
'''

# fullmatch(正则表达式,字符串) 判断正则表达式是否和字符串完全匹配,不匹配返回None
re_result = r'1[3-9]\d{9}'
result = fullmatch(re_result, tel)
print(result)

# 正则表达式的语法
'''
普通字符:普通字符在正则表达式中表示符号本身

'''
# --------------------匹配符号-----------------------
# 匹配一个字符串有三个字符,分别是a,b,c
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))  # 需要导入S
print(fullmatch(r'(?s)a.b','a\nb'))  # 加(?s)

# 多行匹配
print(fullmatch(r'a.b','a\nb',flags=M))  # 需要导入M
print(fullmatch(r'(?m)a.b','a\nb',flags=S)) # 加(?m)
# 忽略大小写
print(fullmatch(r'ab','AB',flags=I))  # 需要导入I
print(fullmatch(r'(?i)ab','ABc'))   #加(?i)
# 忽略大小写并且单行匹配
print(fullmatch(r'a.b','A\nb',flags=I | S)) #加 |
print(fullmatch(r'(?is)a.b','A\nb',flags=I | S)) # 加(?is)或者(?si)
# \d  -  匹配任意一个数字字符
str1 = r'\d\d\d'
result = fullmatch(str1, '780')
print(result)

str1 = r'\d\dabc'
result = fullmatch(str1, '12abc')
print(result)

# \D   匹配任意一个非数字字符
str1 = r'\d\D\D\d'
result = fullmatch(str1, '9a-0')
print(result)

# \s   匹配任意一个空白字符(空格,\n换行,\t制表符)
str1 = r'abc\s123'
print(fullmatch(str1, 'abc 123'))
print(fullmatch(str1, 'abc\n123'))
print(fullmatch(str1, 'abc\t123'))

# \S   匹配任意一个非空白字符
str1 = r'abc\S\d\d\d'
print(fullmatch(str1, 'abc 123'))  # None
print(fullmatch(str1, 'abc-633'))

# \w  匹配任意非ASCII码中的字符(不好用)

# [字符集]  匹配字符集中出现的任意一个字符
'''
一个[]只能匹配一个字符
例:
[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'))  # None
print(fullmatch(str1, 'b12'))  # None
print(fullmatch(str1, 'c12'))  # None
str2 = r'[a^b]123'
print(fullmatch(str2, 'a123'))
print(fullmatch(str2, '^123'))
print(fullmatch(str2, 'b123'))
# --------------------检测符号-----------------------
'''
匹配符号要求一个符号必须对应一个字符,会影响字符串长度的描述
检测符号,不会匹配字符,也不会影响字符串长度
是在匹配成功的前提下对指定位置的字符进行检测

'''
# \b 检测是否是单词边界
'''
单词边界---所有能够区分出两个不同单词的符号,例如 :空白符号,标点符号,字符串开头和结尾
'''
str1 = r'abc\b\s123'
print(fullmatch(str1, 'abc 123'))

# \B 检测是否不是单词边界
str1 = r'abc\B123'
print(fullmatch(str1, 'abc123'))

# ^  检测是否字符串开头
str1 = r'abc^123'  # ^放字符串中间检测,没有能匹配上的字符串
print(fullmatch(str1, 'abc123'))  # None
print(fullmatch(str1, 'abc^123'))  # None

str2 = r'^abc123'
print(fullmatch(str1, 'abc123'))

# search   找到第一个能匹配的子串
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'))

# + 1次或者多次(至少1次)

# ? 0次或1次
# 写一个正则能匹配任意正整数:23,3490,+87
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'))

# 用户名的要求:全部由数字或者字母组成,长度3~6位
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'))  # None

# 贪婪和非贪婪
'''
匹配次数不确定的时候匹配模式有贪婪(默认)和非贪婪(在不确定次数后面加?)两种
贪婪:
在能匹配成功的前提下,匹配次数选最多的

非贪婪:
在能匹配成功的前提下,匹配次数选最少的
'''
# 贪婪
str1 = 'a.+b'
print(search(str1, 'xxa111b2223b12234b'))  # <re.Match object; span=(2, 18), match='a111b2223b12234b'>
# 非贪婪
str1 = 'a.+?b'
print(search(str1, 'xxa111b2223b12234b'))  # <re.Match object; span=(2, 7), match='a111b'>

# --------------------分组-----------------------

# ()
'''
(ab){M,N} ab整体重复M到N次

\M   重复前面的第M个分组中匹配到的内容 

捕获:
re中findall在获取子串的时候,如果正则中有分组,只会获取分组中的内容
'''
# 两个数字两个字母的结构重复3到5次
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'))  # ['a11b', 'a15b']
str1=r'a(\d{2})b'
print(findall(str1,'ahaaba11ba1fba15b')) #['11', '15']

# -------------------分支------------------------
'''
|  正则1|正则2|正则3 
先判断正则1是否成立,成立则匹配成功,不成立继续往后判断,全部不成立那么就匹配失败
'''
# abc后面是三个数字或者是三个大写字母
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'))  # <re.Match object; span=(0, 5), match='15+15'>
str1=r'\d{2}\.\d{2}'
print(fullmatch(str1,'15+15'))  # None
str1=r'(\d{2})'
print(fullmatch(str1,'(15)'))  # None
str1=r'\(\d{2}\)'
print(fullmatch(str1,'(15)'))  # <re.Match object; span=(0, 4), match='(15)'>

'''
. \d \D \s \S \w [] [^] 
$ ^ \B \b
{} ?  *  +  -
() 
|
\
'''





猜你喜欢

转载自blog.csdn.net/weixin_44628421/article/details/109194183