string提供的是完全匹配
s = 'hello world'
ret = s.find('llo') # 找到字母llo,运行后显示位置
print(ret) # 2
ret1 = s.replace('ll','xx') # 用 xx 替换 ll
print(ret1) # hexxo world
ret2 = s.split('w') # 以w为界限,分割字母,但w会被分割出去
print(ret2) # ['hello ', 'orld']
用法:match(pattern, string, flags=0)
pattern: 正则模型
string : 要匹配的字符串
falgs : 匹配模式
用法:search(pattern, string, flags=0)
pattern: 正则模型
string : 要匹配的字符串
falgs : 匹配模式
用法:split(pattern, string, maxsplit=0, flags=0)
pattern: 正则模型
string : 要匹配的字符串
maxsplit:指定分割个数
flags : 匹配模式
用法:findall(pattern, string, flags=0)
pattern: 正则模型
string : 要匹配的字符串
falgs : 匹配模式
ret = re.findall('w..l','hello world')
print(ret) # ['worl']
ret = re.findall('^h...f','hjisfnlhello') #只从最初开始匹配
print(ret) # ['hjisf']
ret = re.findall('e..o$','hjisfnlalexhello')
print(ret) # ['ello']
ret = re.findall('a|b','acbbcbaac')
print(ret) # ['a', 'b', 'b', 'b', 'a', 'a']
ret = re.findall('ba*','afdhfhnandbbbbbaaabaaaaaaaaa')
print(ret) # ['b', 'b', 'b', 'b', 'baaa', 'baaaaaaaaa']
val = re.findall('1.*','11*222223456') # 没有使用转义符,*就会进行贪婪匹配
ret = re.findall('1.\*','11*222223456') # 使用转义符,*就变成了一个普通字符
print(val) # ['11*222223456']
print(ret) # ['11*']
ret1 = re.findall('\\t','asdfa\tsd4654') # 如果要匹配字符串中\t,需要在\t前加\,让\t中的\失去特殊意义
print(ret1) # ['\t']
ret2 = re.findall(r'\t','asdfa\tsd4654') # 也可以在‘’号前加r,让其不进行转义,效果同上
print(ret2) # ['\t']
ret1 = re.findall('ab+','afdhfhnandbabaaaaabbbaaabbaaa')
print(ret1) # ['ab', 'abbb', 'abb']
ret2 = re.findall('a+b','afdhfhnandbabaabaaaaaaaaaab')
print(ret2) # ['ab', 'aab', 'aaaaaaaaaab']
ret = re.findall('a?b','afdhfhnandbabaabbbaaaaaaaabaa') #匹配到0-1个a
print(ret) # ['b', 'ab', 'ab', 'b', 'b', 'ab']
a = re.findall('a{5}b','aaaaaaaaaaaaab') #匹配5个a,一个b
print(a) # ['aaaaab']
b = re.findall('a{1,3}b','aaaaaaabbbbbbbbbb') #至少有一个a才能匹配成功,最多3个a,非贪婪模式
print(b) # ['aaab']
ret = re.findall('[a-z]','adsoAHK12387farSDGKAgag') # 取出字符串中的所有小写字母
print(ret) # ['a', 'd', 's', 'o', 'f', 'a', 'r', 'g', 'a', 'g']
ret1 = re.findall('[A-Z]','adsoAHK12387farSDGKAgag') # 取出字符串中的所有大写字母
print(ret1) # ['A', 'H', 'K', 'S', 'D', 'G', 'K', 'A']
ret2 = re.findall('[0-9]','adsoAHK12387farSDGKAgag') # 取出字符串中的所有数字
print(ret2) # ['1', '2', '3', '8', '7']
ret3 = re.findall('[^A-Z]','adsoAHK12387farSDGKAgag') # ^号表示取反,此处为取所有除大写字母外的字符
print(ret3) # ['a', 'd', 's', 'o', '1', '2', '3', '8', '7', 'f', 'a', 'r', 'g', 'a', 'g']
ret = re.findall('(ab){2}','abababab') # 这里表示2个ab完成一次匹配,匹配出来的内容为ab
print(ret) # ['ab', 'ab']
ret = re.findall('\d','adfadsf123123')
print(ret) # ['1', '2', '3', '1', '2', '3']
ret = re.findall('\d+','ad456456fadsf123123')
print(ret) # ['456456', '123123']
ret = re.findall('\D','ad456__456fadsf123123')
print(ret) # ['a', 'd', '_', '_', 'f', 'a', 'd', 's', 'f']
ret = re.findall('\s','ad456__456 fadsf123\t12\n3')
print(ret) # [' ', ' ', '\t', '\n']
ret = re.findall('\S','ad_456 f13\t12\n3')
print(ret) # ['a', 'd', '_', '4', '5', '6', 'f', '1', '3', '1', '2', '3']
ret = re.findall('\w','ad_456 f13\t12\n3')
print(ret) # ['a', 'd', '_', '4', '5', '6', 'f', '1', '3', '1', '2', '3']
ret = re.findall('\W','ad_456 f13\t12\n3')
print(ret) # [' ', ' ', '\t', '\n']
ret = re.findall('\Aad....','ad_456 f13\t12\n3')
print(ret) # ['ad_456']
ret = re.findall('\d+\Z','ad_456 f13\t12\n333')
print(ret) # ['333']
ret = re.findall(r'\b','\tad_45 6f1312333\n') # 中间的空格是上一个的结尾,第二个的开始,所以会匹配两次
print(ret) # ['', '', '', '']
ret = re.findall('\d\B','ad_456 f13 12 3') # 检索内容不能位于开头或者结尾
print(ret) # ['4', '5', '1', '1']