day22_6-re模块
# 参考资料:
# python模块(转自Yuan先生) - 狂奔__蜗牛 - 博客园
# https://www.cnblogs.com/guojintao/articles/9070485.html
# ------------------------------------------------------------
# ********************day22_6-re模块 *******************
# ********************day22_6-re模块 *******************
# =====>>>>>>内容概览
# =====>>>>>>内容概览
# ------------------------------------------------------------
# # 1、re模块 的解释
# # # 就其本质而言,正则表达式(或 RE)是一种小型的、高度专业化的编程语言,(在Python中)它内嵌
# # # 在Python中,并通过 re 模块实现。正则表达式模式被编译成一系列的字节码,然后由用 C 编写的匹
# # # 配引擎执行。
# # # 字符匹配(普通字符,元字符):
# # # 1 普通字符:大多数字符和字母都会和自身匹配
# # # >>> re.findall('alvin','yuanaleSxalexwupeiqi')
# # # ['alvin']
# # # 2 元字符:. ^ $ * + ? { } [ ] | ( ) \
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 2、re.findall(待匹配的字符串,被查找的对象)
# # # 在字符串中找到正则表达式所匹配的所有子串,并返回一个列表,如果没有找到匹配的,则返回空列表。
# # # 注意: findall 匹配所有。
# # # Python3 正则表达式 | 菜鸟教程
# # # http://www.runoob.com/python3/python3-reg-expressions.html
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 3、re.findall(待匹配的字符串,被查找的对象) 与 “.”元字符
# # # 一个元字符“.”代表一个未知的字符
# # # Python3 正则表达式 | 菜鸟教程
# # # http://www.runoob.com/python3/python3-reg-expressions.html
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 4、re.findall(待匹配的字符串,被查找的对象) 与 “^”元字符
# # # “^”代表待匹配的字符串的内容是在 被查找的对象中的开头部分开始匹配
# # # Python3 正则表达式 | 菜鸟教程
# # # http://www.runoob.com/python3/python3-reg-expressions.html
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 5、re.findall(待匹配的字符串,被查找的对象) 与 “$”元字符
# # # “$”代表的是,待匹配的字符串的位置的末尾处,是在被查找对象的末尾的
# # # Python3 正则表达式 | 菜鸟教程
# # # http://www.runoob.com/python3/python3-reg-expressions.html
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 6、re.findall(待匹配的字符串,被查找的对象) 与 元字符“*”
# # # 其前面的一个字符匹配0个或多个的表达式。
# # # Python3 正则表达式 | 菜鸟教程
# # # http://www.runoob.com/python3/python3-reg-expressions.html
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 7、re.findall(待匹配的字符串,被查找的对象) 与 元字符“+”
# # # 其前面的一个字符匹配1个或多个的表达式。
# # # Python3 正则表达式 | 菜鸟教程
# # # http://www.runoob.com/python3/python3-reg-expressions.html
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 8、re.findall(待匹配的字符串,被查找的对象) 与 元字符“?”
# # # 其前面的一个字符匹配1个或0个的表达式。
# # # Python3 正则表达式 | 菜鸟教程
# # # http://www.runoob.com/python3/python3-reg-expressions.html
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 9、re.findall(待匹配的字符串,被查找的对象) 与 元字符“{}”
# # # {0,} == *
# # # {1,} == +
# # # {0,1} == ?
# # # {6} ==
# # # Python3 正则表达式 | 菜鸟教程
# # # http://www.runoob.com/python3/python3-reg-expressions.html
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 10、re.findall(字符序列1[字符序列2],被查找的对象) 与 元字符“[]”
# # # 字符序列2提供选择,查找是,字符序列1在序列2中选出某个字符进行搭配,之后再被查找对象中进行查找
# # # Python3 正则表达式 | 菜鸟教程
# # # http://www.runoob.com/python3/python3-reg-expressions.html
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 11、re.findall(字符序列1[字符序列2],被查找的对象) 与 元字符“[ - ]*”
# # # 字符序列2提供选择,查找是,字符序列1在序列2中选出某个字符进行搭配,之后再被查找对象中进行查找
# # # 里面的“-”表示范围,[ a-z] ,表示a-z的小写字母;[ 0-9] ,表示a-z的小写字母
# # # Python3 正则表达式 | 菜鸟教程
# # # http://www.runoob.com/python3/python3-reg-expressions.html
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 12、re.findall(字符序列1[^字符序列2],被查找的对象) 与 元字符“[^ - ]”
# # # ^字符序列2进行取非,之后提供选择,查找是,字符序列1在序列2中选出某个字符进行搭配,
# # # 之后再被查找对象中进行查找
# # # 里面的“^”表示取非,[ a-z] ,表示a-z的小写字母;[ 0-9] ,表示a-z的小写字母
# # # Python3 正则表达式 | 菜鸟教程
# # # http://www.runoob.com/python3/python3-reg-expressions.html
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 13、元字符之转义符\
# # # Python3 正则表达式 | 菜鸟教程
# # # http://www.runoob.com/python3/python3-reg-expressions.html
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 14、re.findall 在计算中的应用
# # # 在计算上的应用,使用正则表达示来处理字符串,而不是用eval来去掉字符中的双引号来实现计算
# # # 这里是将内层的(2-1)取出来
# # # Python3 正则表达式 | 菜鸟教程
# # # http://www.runoob.com/python3/python3-reg-expressions.html
# ------------------------------------------------------------
------------------------------------------------分割线-------------------------------------------------
------------------------------------------------分割线-------------------------------------------------
------------------------------------------------分割线-------------------------------------------------
''' # ------------------------------------------------------------ # # 1、re模块 的解释 # # # 就其本质而言,正则表达式(或 RE)是一种小型的、高度专业化的编程语言,(在Python中)它内嵌 # # # 在Python中,并通过 re 模块实现。正则表达式模式被编译成一系列的字节码,然后由用 C 编写的匹 # # # 配引擎执行。 # # # 字符匹配(普通字符,元字符): # # # 1 普通字符:大多数字符和字母都会和自身匹配 # # # >>> re.findall('alvin','yuanaleSxalexwupeiqi') # # # ['alvin'] # # # 2 元字符:. ^ $ * + ? { } [ ] | ( ) \ # ------------------------------------------------------------ ''' ''' # ------------------------------------------------------------ # # 2、re.findall(待匹配的字符串,被查找的对象) # # # 在字符串中找到正则表达式所匹配的所有子串,并返回一个列表,如果没有找到匹配的,则返回空列表。 # # # 注意: findall 匹配所有。 # # # Python3 正则表达式 | 菜鸟教程 # # # http://www.runoob.com/python3/python3-reg-expressions.html # ------------------------------------------------------------ ''' # # import re # a1 = re.findall("alex","asfasgasdfalrex") # a2 = re.findall("alex","asfasgasdfalex") # print(a1) # print(a2) # # # D:\Anaconda3\python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_6-re.py # # [] # # ['alex'] # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 3、re.findall(待匹配的字符串,被查找的对象) 与 “.”元字符 # # # 一个元字符“.”代表一个未知的字符 # # # Python3 正则表达式 | 菜鸟教程 # # # http://www.runoob.com/python3/python3-reg-expressions.html # ------------------------------------------------------------ ''' # # import re # a1 = re.findall("a.x","asfasgasdfalex") # a2 = re.findall("a..x","asfasgasdfalex") # a3 = re.findall("a....x","asfasgasdfalex") # # print(a1) # print(a2) # print(a3) # # # D:\Anaconda3\python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_6-re.py # # [] # # ['alex'] # # [] # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 4、re.findall(待匹配的字符串,被查找的对象) 与 “^”元字符 # # # “^”代表待匹配的字符串的内容是在 被查找的对象中的开头部分开始匹配 # # # Python3 正则表达式 | 菜鸟教程 # # # http://www.runoob.com/python3/python3-reg-expressions.html # ------------------------------------------------------------ ''' # # import re # a0 = re.findall("^a","alexsfasgasdfalex") # a1 = re.findall("^a.x","alexsfasgasdfalex") # a2 = re.findall("^a..x","alexsfasgasdfalex") # a21 = re.findall("^a..x","wwwalexsfasgasdfalex") # 被查找的字符串的开头不是a # a3 = re.findall("^a....x","alexsfasgasdfalex") # # print(a0) # print(a1) # print(a2) # print(a21) # print(a3) # # # # D:\Anaconda3\python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_6-re.py # # ['a'] # # [] # # ['alex'] # # [] # # [] # # ['x'] # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 5、re.findall(待匹配的字符串,被查找的对象) 与 “$”元字符 # # # “$”代表的是,待匹配的字符串的位置的末尾处,是在被查找对象的末尾的 # # # Python3 正则表达式 | 菜鸟教程 # # # http://www.runoob.com/python3/python3-reg-expressions.html # ------------------------------------------------------------ ''' # # import re # a0 = re.findall("x$","alexsfasgasdfalex") # a1 = re.findall("a.x$","alexsfasgasdfalex") # a2 = re.findall("a..x$","alexsfasgasdfalex") # a21 = re.findall("a..x$","alexsfasgasdfalexwwwww") # a3 = re.findall("a....x$","alexsfasgasdfalex") # # print(a0) # print(a1) # print(a2) # print(a21) # print(a3) # # # # # D:\Anaconda3\python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_6-re.py # # ['x'] # # [] # # ['alex'] # # [] # # [] # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 6、re.findall(待匹配的字符串,被查找的对象) 与 元字符“*” # # # 其前面的一个字符匹配0个或多个的表达式。 # # # Python3 正则表达式 | 菜鸟教程 # # # http://www.runoob.com/python3/python3-reg-expressions.html # ------------------------------------------------------------ ''' # # import re # a0 = re.findall("9*","9le99xsfa999sga9999sdfal99999ex") # # 结果表示从 x 出现的次数从0次-->无穷次 # a1 = re.findall("alex*","ale0alex122alexx333alexxx4444alexxxx55555") # # print(a0) # print(a1) # # # D:\Anaconda3\python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_6-re.py # # ['9', '', '', '99', '', '', '', '', '999', '', '', '', '9999', '', '', '', '', '', '99999', '', '', ''] # # ['ale', 'alex', 'alexx', 'alexxx', 'alexxxx'] # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 7、re.findall(待匹配的字符串,被查找的对象) 与 元字符“+” # # # 其前面的一个字符匹配1个或多个的表达式。 # # # Python3 正则表达式 | 菜鸟教程 # # # http://www.runoob.com/python3/python3-reg-expressions.html # ------------------------------------------------------------ ''' # # import re # a0 = re.findall("9+","9le99xsfa999sga9999sdfal99999ex") # # 结果表示从 x 出现的次数从1次-->无穷次 # a1 = re.findall("alex+","ale0alex122alexx333alexxx4444alexxxx55555") # # print(a0) # print(a1) # # # D:\Anaconda3\python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_6-re.py # # ['9', '99', '999', '9999', '99999'] # # ['alex', 'alexx', 'alexxx', 'alexxxx'] # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 8、re.findall(待匹配的字符串,被查找的对象) 与 元字符“?” # # # 其前面的一个字符匹配1个或0个的表达式。 # # # Python3 正则表达式 | 菜鸟教程 # # # http://www.runoob.com/python3/python3-reg-expressions.html # ------------------------------------------------------------ ''' # # import re # a0 = re.findall("9?","9le99xsfa999sga9999") # # 结果表示从 x 出现的次数从0次-->1次 # a1 = re.findall("alex?","ale0alex122alexx333alexxx4444alexxxx55555") # # print(a0) # print(a1) # # # D:\Anaconda3\python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_6-re.py # # ['9', '', '', '9', '9', '', '', '', '', '9', '9', '9', '', '', '', '9', '9', '9', '9', ''] # # ['ale', 'alex', 'alex', 'alex', 'alex'] # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 9、re.findall(待匹配的字符串,被查找的对象) 与 元字符“{}” # # # {0,} == * # # # {1,} == + # # # {0,1} == ? # # # {6} == # # # Python3 正则表达式 | 菜鸟教程 # # # http://www.runoob.com/python3/python3-reg-expressions.html # ------------------------------------------------------------ ''' # # import re # # # {0,} == * ,结果表示从 x 出现的次数从0次-->无穷次 # a0 = re.findall("alex{0,}","ale0alex122alexx333alexxx4444alexxxx55555") # # # {1,} == + ,结果表示从 x 出现的次数从1次-->无穷次 # a1 = re.findall("alex{1,}","ale0alex122alexx333alexxx4444alexxxx55555") # # # {0,1} == ? ,结果表示从 x 出现的次数从0次-->1次 # a2 = re.findall("alex{0,1}","ale0alex122alexx333alexxx4444alexxxx55555") # # # {1,3} == {1,3} ,结果表示从 x 出现的次数从0次-->无穷次 ?? # a3 = re.findall("alex{1,3}","ale0alex122alexx333alexxx4444alexxxx55555") # # # {3} == {0,3} ,结果表示从 x 出现的次数从0次-->无穷次 ?? # a4 = re.findall("alex{0,}","ale0alex122alexx333alexxx4444alexxxx55555") # # print("alex{?,?}".center(60,"-")) # print("{0,} == * ",a0) # print("{1,} == + ",a1) # print("{0,1} == ? ",a2) # print("{1,3} == {} ",a3) # print("{3} == {1,3}",a4) # # # D:\Anaconda3\python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_6-re.py # # -------------------------alex{?,?}-------------------------- # # {0,} == * ['ale', 'alex', 'alexx', 'alexxx', 'alexxxx'] # # {1,} == + ['alex', 'alexx', 'alexxx', 'alexxxx'] # # {0,1} == ? ['ale', 'alex', 'alex', 'alex', 'alex'] # # {1,3} == {} ['alex', 'alexx', 'alexxx', 'alexxx'] # # {3} == {1,3} ['ale', 'alex', 'alexx', 'alexxx', 'alexxxx'] # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 10、re.findall(字符序列1[字符序列2],被查找的对象) 与 元字符“[]” # # # 字符序列2提供选择,查找是,字符序列1在序列2中选出某个字符进行搭配,之后再被查找对象中进行查找 # # # Python3 正则表达式 | 菜鸟教程 # # # http://www.runoob.com/python3/python3-reg-expressions.html # ------------------------------------------------------------ ''' # import re # a1 = re.findall("x[123]","x123aaax2aax3xx23") # a2 = re.findall("x[123]A","x1A23aaax2Aaax3Axx2A3") # a3 = re.findall("x[1,2,3]A","x1A23aaax23Aaax13Axx123A3") # a4 = re.findall("x[1,2,*]A","x1A23aaax,Aaax*Axx123A3") # # print(a1) # print(a2) # print(a3) # print(a4) # # # # D:\Anaconda3\python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_6-re.py # # ['x1', 'x2', 'x3', 'x2'] # # ['x1A', 'x2A', 'x3A', 'x2A'] # # ['x1A'] # # ['x1A', 'x,A', 'x*A'] # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 11、re.findall(字符序列1[字符序列2],被查找的对象) 与 元字符“[ - ]*” # # # 字符序列2提供选择,查找是,字符序列1在序列2中选出某个字符进行搭配,之后再被查找对象中进行查找 # # # 里面的“-”表示范围,[ a-z] ,表示a-z的小写字母;[ 0-9] ,表示a-z的小写字母 # # # Python3 正则表达式 | 菜鸟教程 # # # http://www.runoob.com/python3/python3-reg-expressions.html # ------------------------------------------------------------ ''' # # import re # a1 = re.findall("x[0-9]*","x123aaax2aax3xx23") # a2 = re.findall("x[0-9]*A","x1A23aaax2Aaax3Axx222A3") # # print(a1) # print(a2) # # # D:\Anaconda3\python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_6-re.py # # ['x123', 'x2', 'x3', 'x', 'x23'] # # ['x1A', 'x2A', 'x3A', 'x222A'] # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 12、re.findall(字符序列1[^字符序列2],被查找的对象) 与 元字符“[^ - ]” # # # ^字符序列2进行取非,之后提供选择,查找是,字符序列1在序列2中选出某个字符进行搭配, # # # 之后再被查找对象中进行查找 # # # 里面的“^”表示取非,[ a-z] ,表示a-z的小写字母;[ 0-9] ,表示a-z的小写字母 # # # Python3 正则表达式 | 菜鸟教程 # # # http://www.runoob.com/python3/python3-reg-expressions.html # ------------------------------------------------------------ ''' # # import re # a0 = re.findall("x[^0-9]*","xx23") # 取次数最多 # a1 = re.findall("x[^0-9]*","x123aaax2aax3xx23") # a2 = re.findall("x[^0-9]*A","xaA23aaaxWAaax3Axx222A3") # # print(a0) # print(a1) # print(a2) # # # D:\Anaconda3\python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_6-re.py # # ['xx'] # # ['x', 'x', 'x', 'xx'] # # ['xaA', 'xWA'] # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 13、元字符之转义符\ # # # Python3 正则表达式 | 菜鸟教程 # # # http://www.runoob.com/python3/python3-reg-expressions.html # ------------------------------------------------------------ ''' # # import re # data = "12+(34*6+2-5*(2-1))" # r1 = re.findall("\d",data) # 匹配任意数字,等价于 [0-9]。 # r11 = re.findall("[0-9]",data) # r2 = re.findall("\d+",data) # r3 = re.findall("[0-9]+",data) # r4 = re.findall("\D",data) # 匹配任意非数字 # r5 = re.findall("\D+","hello world 1234ww") # r6 = re.findall("\s+",data) # 匹配任意空白字符,等价于 [\t\n\r\f]。 # # # print("\d ",r1) # print("[0-9] ",r11) # print("\d+ ",r2) # print("[0-9]+ ",r3) # print("\D ",r4) # print("\D+ ",r5) # print("\s+ ",r6) # # # D:\Anaconda3\python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_6-re.py # # \d ['1', '2', '3', '4', '6', '2', '5', '2', '1'] # # [0-9] ['1', '2', '3', '4', '6', '2', '5', '2', '1'] # # \d+ ['12', '34', '6', '2', '5', '2', '1'] # # [0-9]+ ['12', '34', '6', '2', '5', '2', '1'] # # \D ['+', '(', '*', '+', '-', '*', '(', '-', ')', ')'] # # \D+ ['hello world ', 'ww'] # # \s+ [] # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 14、re.findall 在计算中的应用 # # # 在计算上的应用,使用正则表达示来处理字符串,而不是用eval来去掉字符中的双引号来实现计算 # # # 这里是将内层的(2-1)取出来 # # # Python3 正则表达式 | 菜鸟教程 # # # http://www.runoob.com/python3/python3-reg-expressions.html # ------------------------------------------------------------ ''' # # import re # data = "12+(34*6+2-5*(2-1))" # # eval处理 # e = eval(data) # r = re.findall("\([^()]*\)",data) # # print(e) # print(r,type(r),type(r[0])) # # # D:\Anaconda3\python.exe D:/C_cache/py/day22_os_json_re_etc_MoKuai/day22_6-re.py # # 213 # # ['(2-1)'] <class 'list'> <class 'str'> # # # # Process finished with exit code 0
转载请注明出处。https://www.cnblogs.com/jyfootprint/p/9457346.html