正则法则 re模块

1.匹配不到返回空列表

s = "meet_宝元_me@et\t \n"
print(re.findall("\w",s))  #匹配字母.数字.下划线和中文
# s = "meet_宝元_me@et\t \n"
# print(re.findall("\s",s)) #匹配任意空格,制表符,换行符

# s = "meet_宝元_me@et\t \n"
# print(re.findall("\S",s)) #匹配非空格,制表符,换行符

# s = "meet_宝元_me@et\t \n123"
# print(re.findall("\d",s))  #匹配任意数字

# s = "meet_宝元_me@et\t \n123"
# print(re.findall("\D",s)) #匹配非数字

# s = "meet_宝元_me@et\t \n123"
# print(re.findall("\Am",s)) #正规,写在右侧,['m']
# print(re.findall("^m",s)) #正规,写在右侧,['m']
# print(re.findall("\Ae",s)) #正规,不是以e开头,返回空列表[]
# print(re.findall("\A",s))   #什么都不写默认以空(什么都没有)开头['']
# print(re.findall("m\A",s)) #写在右侧无论什么返回都是空列表[]

# s = "meet_宝元_me@et \n \t123"
# print(re.findall("3\Z",s)) #正规,写在左侧,['3']
# print(re.findall("3$",s)) #正规,写在左侧,['3']

# s = "meet_宝元_me@et\t \n123"
# print(re.findall("\n",s)) #匹配换行符['\n']

# s = "meet_宝元_me@et\t \n123"
# print(re.findall("\t",s)) #匹配制表符['\t']

# s = "meet_宝元_me@et\t \n123"
# print(re.findall(".",s)) #匹配任意字符,除了换行符
# print(re.findall(".",s,re.DOTALL)) #匹配任意字符,包括换行符

# s = "meet_宝元_mE@et\t \n123"
# print(re.findall("[a-z]",s)) #从小a到小z
# print(re.findall("[A-Z]",s)) #从大 A到大Z
# print(re.findall("[0-9]",s)) #从 0到9
# print(re.findall("[a-z0-9]",s)) #从小a到小z从 0到9

# s = "meet_太白_me#Et\t \n123"
# print(re.findall("...",s)) #按.的个数匹配所有字符

# s = "mmmeet_太白_me#Et\t \n123"
# print(re.findall("me*",s))  #匹配*左边元素0个或多个
# #['m', 'm', 'mee', 'me']  #注意0的含义

# s = "mmmeet_宝元_me#Et\t \n 123"
# print(re.findall("me+",s)) # 匹配+前面元素1个或多个
#['mee', 'me'] #注意1的含义

# s = "mmmeet_宝元_me#Et\t \n 123"
# #{n,m}-------------------------匹配n到m个元素
# print(re.findall("e{1,2}",s))  #注意{}中的起始终止数字

# s = "alex_太白_riTian\t \n234"
# print(re.findall(".*",s)) #匹配除了换行符外的0个或多个
# # ['alex_太白_riTian\t ', '', '234', '']  #注意最后还有一个空
#
# name = "m-e-me-meet-meet_123\t \n"
# print(re.findall(".*",name))
# # ['m-e-me-meet-meet_123\t ', '', '']

# s = "alex_太白_riTianii\t \n234"
# print(re.findall(".*?i",s))
# #['alex_太白_ri', 'Ti', 'ani', 'i']  #前边任意0个或多个,以i结尾
#
# s = "alex_太白_riTiani\t \n234"
# print(re.findall("a.*?",s))
# # ['a', 'a']
#
# s = "alex_太白_riTiani\t \n234"
# print(re.findall("a.*?i",s))
#['alex_太白_ri', 'ani'] #以a开头以i结尾0个或中间任意多个

# s = "alex_太白_riTiani\t \n234"
# print(re.findall("[1-9]",s))  #['2', '3', '4']
# print(re.findall("[a-z]",s))  #['a', 'l', 'e', 'x', 'r', 'i', 'i', 'a', 'n', 'i']
# print(re.findall("[^1-9]",s))  #['a', 'l', 'e', 'x', '_', '太', '白', '_', 'r', 'i', 'T', 'i', 'a', 'n', 'i', '\t', ' ', '\n']
# s = "alex_太白_riTiani\t \n234"
# print(re.findall("[1-9]",s))  #['2', '3', '4']
# print(re.findall("[a-z]",s))  #['a', 'l', 'e', 'x', 'r', 'i', 'i', 'a', 'n', 'i']
# print(re.findall("[^1-9]",s))  #['a', 'l', 'e', 'x', '_', '太', '白', '_', 'r', 'i', 'T', 'i', 'a', 'n', 'i', '\t', ' ', '\n']

# 有如下字符串:'alex_sb ale123_sb wu12sir_sb wusir_sb ritian_sb' 的 alex wusir '
# 找到所有带_sb的内容
# s = 'alex_sb ale123_sb wu12sir_sb wusir_sb ritian_sb 的 alex wusir '
# print(re.findall(".*?_sb",s))

# s = "alex_太白_riTiani\t \n234"
# print(re.findall("(.*?)i",s))  #以i分割,没有i
#['alex_太白_r', 'T', 'an'] #只匹配括号里面的

# s = "alex_太白_riTiani\t \n234"
# print(re.findall("(.*?)i",s))  #以i分割,没有i
#['alex_太白_r', 'T', 'an'] #只匹配括号里面的

# s = "alex_太白_riTiani\t \n234"
# print(re.findall("a(?:.*?)i",s)) #['alex_太白_ri', 'ani']
# print(re.findall("a(?:.*)i",s)) #['alex_太白_riTiani']

# s = "meet_assdf_mssst_(.)mmns_aaamaaatmsssssssssssstt"
# print(re.findall("m(?:.*?)t",s)) #['meet', 'mssst', 'mmns_aaamaaat', 'msssssssssssst']

# s = "alex_太白_riTiani\t \n234"
# print(re.findall("a|i",s))  #['a', 'i', 'i', 'a', 'i'] 匹配a或i

# print(re.findall('(..day|morrow)','Work harder today than yesterday, and the day after tomorrow will be better'))
# findall 全部找到返回一个列表
# search 从字符串任意位置进行匹配查找,查找到一个就停止,
# 返回的是一个对象,获取匹配的内容必须使用.group(进行获取)
# s = "alex_太白_riTiani\t \n234"
# print(re.search("i",s).group())
#match 从字符串开始位置进行匹配
# s = "alex_太白_riTiani\t \n234"
# print(re.match("al",s).group())  #从开头匹配

# split 分割 可按照任意分隔符进行分割
# s = "alex_太白_riTiani\t \n234"
# print(re.split("[_]",s))

#sub 替换
# s = "alex_太白_riTiani 234"
# print(re.sub("i","s",s))

#compile 定义匹配规则
# s = "alex234tidgf"
# # obj = re.compile("\w{2}")
# # print(obj.findall(s))

#finditer 返回一个迭代器
# s = "alex234alex"
# g = re.finditer("l",s)
# for i in g:
#     print(i.group())

# # 1.1 匹配所有的整数
# l = "1-2*(60+(-401.35/5)-(-4*3))"
# print(re.findall("\d+",l))

# 匹配所有的数字(包含小数)
# s = "1-2*(60+(-40.35/5)-(-4*3))"
# print(re.findall("\d+\.\d+|\d+",l))

# 匹配所有的数字(包含小数包含负号)
# s = "1-2*(60+(-40.35/5)-(-4*3))"
# print(re.findall("-\d+\.\d+|-?\d+",s))

# s = "http://blog.csdn.net/make164492212/article/details/51656638"
# print(re.findall("h.*2",s))

s1 = '''
时间就是1995-04-27,2005-04-27
1999-04-27 老男孩教育创始人
老男孩老师 alex 1980-04-27:1980-04-27
2018-12-08
'''
print(re.findall("\d+-\d+-\d+",s1))

# 匹配qq号:腾讯从10000开始
# num = input("请输入qq号")
# print(re.findall("[1-9][0-9]{4,9}",num))

猜你喜欢

转载自www.cnblogs.com/lvweihe/p/11276043.html
今日推荐