# 匹配字符串可以使用match() search() findall()等方法 # match()从字符串的开始处进行匹配,如果在起始位置匹配成功,则返回match()对象,否则返回None # re.match(pattern, string, [flags]) # pattern 模式字符串 # string 要匹配的字符串 # flags 标志位,用于控制匹配方式,如是否区分大小写,常用标志位如下: # # A 或 ASCII 对于\w \W \b \B \d \D \s \S只进行ASCII匹配(仅适用于python3.x) # I 或 IGNORECASE 执行字母不区分大小写的匹配 # M 或 MULTILINE 将^和$用于包括整个字符串的开始和结尾的每一行(默认情况下,仅适用于整个字符串的开始和结尾处) # S 或 DOTALL 使用(.)字符匹配所有字符,包括换行符 # X 或 VERBOSE 忽略模式字符串中未转义的空格和注释 # # 例:匹配字符串是否以“mr_”开关,不区分字母大小写,代码如下: import re pattern = r‘mr_\w+‘ string = ‘MR_SHOP mr_shop‘ match = re.match(pattern, string, re.I) print(match) string = ‘项目名称MR_SHOP mr_shop‘ match = re.match(pattern, string, re.I) print(match) # 由此可知, ‘MR_SHOP mr_shop‘以mr_开头,将返回一个Match对象,而‘项目名称MR_SHOP mr_shop‘没有以mr_开头,直接返回None # 因为match()方法从开始位置开始匹配,当第一个字符不符合条件时,直接返回None string = ‘MR_SHOP mr_shop‘ match = re.match(pattern, string, re.I) print(‘匹配值的起始位置:‘,match.start()) print(‘匹配值的结束位置:‘,match.end()) print(‘匹配位置的元组:‘,match.span()) print(‘匹配字符串:‘,match.string) print(‘匹配数据:‘,match.group()) # 实例:验证输入的手机号码是否为中国移动的号码 pattern = r‘(13[4-9]\d{8})|(15[01289]\d{8})$‘ mobile = ‘13634222222‘ match = re.match(pattern,mobile) # 进行模式匹配 if match == None: # 判断是否为None,为真表示匹配失败 print(mobile,‘不是有效的中国移动手机号码。‘) else: print(mobile,‘是有效的中国移动手机号码。‘) mobile = ‘13144222221‘ match = re.match(pattern,mobile) # 进行模式匹配 if match == None: # 判断是否为None,为真表示匹配失败 print(mobile,‘不是有效的中国移动手机号码。‘) else: print(mobile,‘是有效的中国移动手机号码。‘) # 2 使用search()方法进行匹配 # re.search(pattern, string, [flags]) # 实例:验证是否出现危险字符 pattern = r‘(黑客)|(抓包)|(监听)|(Trojan)‘ # 模式字符串 about = ‘我是一名程序员,我喜欢看黑客方面的图书,想研究一下Trojan。‘ match = re.search(pattern,about) # 进行模式匹配 if match == None: # 判断是否为None,为真表示匹配失败 print(about,‘@ 安全!‘) else: print(about,‘@ 出现了危险词汇!‘) about = ‘我是一名程序员,我喜欢客看计算机网络方面的图书,喜欢开发网站。‘ match = re.match(pattern,about) # 进行模式匹配 if match == None: # 判断是否为None,为真表示匹配失败 print(about,‘@ 安全!‘) else: print(about,‘@ 出现了危险词汇!‘) # 使用findall()方法进行匹配 # 如果匹配成功则返回包含匹配结构的列表,否则返回空列表。 # re.findall(pattern, string, [flags]) # 如果在指定的模式字符串中,包含分组,则返回与分组匹配的文本列表: pattern = r‘[1-9]{1,3}(\.[0-9]{1,3}){3}‘ string = ‘127.0.0.1 192.168.1.66‘ match = re.findall(pattern,string) print(match) # 如果想获取整个模式字符串的匹配,要将整个模式字符串使用一对小括号进行分组。 pattern = r‘([1-9]{1,3}(\.[0-9]{1,3}){3})‘ match = re.findall(pattern,string) print(match) for i in match: # 获取想要的IP地址 print(i[0]) # 3 替换字符串 # re.sub(pattern, rep1, string, count, flags) # 例:隐藏中奖信息中手机号码 pattern = r‘1[34578]\d{7}‘ string = ‘中奖号码:84978981 联系电话:13616595107‘ result = re.sub(pattern, ‘1******‘, string) print(result) # 实例:替换出现的危险字符 pattern = r‘(黑客)|(抓包)|(监听)|(Trojan)‘ # 模式字符串 about = ‘我是一名程序员,我喜欢看黑客方面的图书,想研究一下Trojan。\n‘ sub = re.sub(pattern,‘@_@‘,about) # 进行模式替换 print(sub) about = ‘我是一名程序员,我喜欢看计算机网络方面的图书,喜欢开发网站。‘ sub = re.sub(pattern,‘@_@‘,about) # 进行模式替换 print(sub) # 3 使用正则表达式分割字符串 # re.split(pattern, string, [maxsplit], [flags]) # maxsplit 表示最大的拆分次数 # 从给定的URL中提取出请求地址和各个参数,代码如下: pattern = r‘[?|&]‘ url = ‘http://www.zack.com/login.jsp?username="mr"&pwd="zack"‘ result = re.split(pattern, url) print(result) # ‘@zack @ladygaga @func‘ 可以用r‘\s*@‘分割
原文:https://www.cnblogs.com/zack6688/p/13521527.html