1, 单个位置的字符串提取
1.1 贪婪和非贪婪
import restr = "0a没有?是非贪婪b加上?是贪婪模式b匹配最远的b"print(re.findall(r"a(.+?)b",str))#['没有?是非贪婪b加上?是贪婪模式b匹配最远的']#['没有?是非贪婪']
1.2 用 unicode 匹配中文标点
import recontent='【神州租车】'#这个正则表达式unicode编码# 3010【# 3011 】regex_pattern = r".*\u3010(.+)\u3011.*"print(re.findall(pattern=regex_pattern, string=content))#['神州租车']
2, 连续多个位置的字符串提取
2.1 groupdict
s='2020-02-06'reg=re.compile(r'(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})')regMatch = reg.match(s)if regMatch: print(regMatch.groupdict())else: print('没有匹配上')
2.2 贪婪和非贪婪
if __name__ == '__main__': fmt = "%(asctime)s - %(filename)s:%(lineno)s - %(name)s - [%(levelname)s] - %(message)s" # 忽略大小写,提取所有括号内的内容 regex = re.compile(r"\((.+?)\)", re.IGNORECASE) # 这里的提取,用到了阻止贪婪?,忽略大小写,.+?没有问号匹配的不对 x=regex.findall(fmt) print(x)['asctime', 'filename', 'lineno', 'name', 'levelname', 'message']