# -*-coding:utf-8 -*- import re scerct_code = 'qwxxlxxerxxoxxtyxxvxxuixxexxop' scerct_code_0 = 'qwxxlxxerxx' \ 'oxxtyxxvxxuixxexxop' a = re.findall('q.',scerct_code)#.占位符,匹配任意字符除了换行符/n b = re.findall('q*',scerct_code)#*匹配前一个字符0or无穷次 c = re.findall('q?',scerct_code)#?匹配前一个字符0or1次 #findall匹配所有并返回 #Search匹配第一个,用group()参数表示第几个 #Sub替换符合,返回替换值 print re.findall('xx.*xx',scerct_code) print re.findall('xx.*?xx',scerct_code) print re.findall('xx(.*?)xx',scerct_code) print re.findall('xx(.*?)xx',scerct_code_0,re.S)#re.S粗略理解为换行匹配