正则法则 re模块

1.匹配不到返回空列表

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

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

# s = "meet_宝元[email protected]\t \n123"
# print(re.findall("\d",s))  #匹配任意数字

# s = "meet_宝元[email protected]\t \n123"
# print(re.findall("\D",s)) #匹配非数字

# s = "meet_宝元[email protected]\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_宝元[email protected] \n \t123"
# print(re.findall("3\Z",s)) #正规,写在左侧,['3']
# print(re.findall("3$",s)) #正规,写在左侧,['3']

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

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

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

# s = "meet_宝元[email protected]\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))

原文地址:https://www.cnblogs.com/lvweihe/p/11276043.html

时间: 2024-08-02 19:48:14

正则法则 re模块的相关文章

常用的正则法则实例

正则法则实例: //邮箱 - (BOOL) validateEmail:(NSString *)email { NSString *emailRegex [email protected]"[A-Z0-9a-z._%+-][email protected][A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; NSPredicate *emailTest = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@"

day18 正则和re模块

# 正则# 正则表达式 ---- 通用的 ,处理 字符串# 正则是一种处理文字的规则# 给我们提供一些规则,让我们从杂乱无章的文字中提取有效信息 #模块# 它只是我们使用python去操作一些问题的工具而已,和要操作的这个东西本身是两件事情 # re模块 -- python使用正则# 正则规则 # []在正则里代表字符组 只约束一个字符# 比如[123456789] 没有顺序# 只约束里边其中一个数字 #[字符组]:表示在一个字符的位置可以出现的所有情况中的集合就是一个字符组#表示数字的字符组[

正则、re模块

字符组 : [字符组] 在同一个位置可能出现的各种字符组成了一个字符组,在正则表达式中用[]表示 字符分为很多类,比如数字.字母.标点等等. 假如你现在要求一个位置"只能出现一个数字",那么这个位置上的字符只能是0.1.2...9这10个数之一. 正则 待匹配字符 匹配结果 说明 [0123456789] 8 True 在一个字符组里枚举合法的所有字符,字符组里的任意一个字符和"待匹配字符"相同都视为可以匹配 [0123456789] a False 由于字符组中没

正则,re模块

一.正则表达式(精准匹配) 匹配字符串内容的一种规则 二.字符组 在同一个位置可能出现的各种字符组成了一个字符组,在正则表达式中用[]表示 常见字符组格式如下:[0123456789],[0-9],[a-z],[A-Z],必须由从小到大,不可逆序. 三.主要元字符 [^s]除了s不能匹配 a|b == [ab] 表示匹配任意字符:[\w\W],[\d\D],[\s\S] 熟记(.,\w , \d ,^ ,$ ,a|b , () ,[...] ,[^...]) 元字符 匹配内容 元字符 匹配内容

正则及re模块-基础(一)

正则表达式 一说规则我已经知道你很晕了,现在就让我们先来看一些实际的应用.在线测试工具 http://tool.chinaz.com/regex/   http://tool.oschina.net/regex 首先你要知道的是,谈到正则,就只和字符串相关了.在我给你提供的工具中,你输入的每一个字都是一个字符串.其次,如果在一个位置的一个值,不会出现什么变化,那么是不需要规则的. 比如你要用"1"去匹配"1",或者用"2"去匹配"2&q

正则与re模块

一.正则表达式 在线测试工具 http://tool.chinaz.com/regex/ 1.字符组 在同一个位置可能出现的各种字符组成一个字符组,在正则表达中用[ ]表示 一个正则就是一条匹配规则,可以规定一次匹配字符的长度,字符组每次匹配一个长度为1的字符,例如:待匹配字符为:2a+ 使用字符组进行匹配时就会进行三次比较 正则 待匹配字段 匹配结果 说明 [0123456789] 5 5 在一个字符组里枚举所有合法的字符,待匹配字符与其中一个相同,即便匹配成功 [0123456789] 56

python 带正则的search 模块

glob  是python 提供的一个支持正则表达式的查找文件的模块. 实现上采用了os.listdir() 和 fnmatch.fnmatch().  但是没有真的invoking a subshell. glob.glob(pathname) Return a possibly-empty list of path names that match pathname, which must be a string containing a path specification. pathna

正则和re模块

正则表达式是什么?能做什么? 1,把一个文件中所有的手机号码都找出来      从大段的文字中找到符合规则的内容 open打开文件 读文件  str 从一长串的字符串中找到所有的11位数字 是一个字符一个字符的读 2.输入手机号                                          判断某个字符串是否完全符合规则 验证这个手机号是否合法 给这个手机号发送一个验证码 用户收到验证码  填写验证码 完成注册 正则表达式 从大段的文字中找到符合规则的内容 爬虫   从网页的

正则法则验证邮箱和手机号码

import java.util.regex.Matcher; import java.util.regex.Pattern; public class CheckMobileAndEmail {  /**  *   * 验证邮箱地址是否正确  *   * @param email  *   * @return  */  public static boolean checkEmail(String email) {   boolean flag = false;   try {    Stri