python的re(正则表达式)
python re
1
import re s = '''bottle\nbag\nbig\napple''' for i,c in enumerate(s, 1): print((i-1, c), end="\n" if i%8==0 else ' ') print() print('--match--') result = re.match('b', s) print(1, result) result = re.match('a', s) print(2, result) result = re.match('^a', s, re.M) print(3, result) result = re.match('^a', s, re.S) print(4, result) # 先编译,然后使用正则表达式对象 regex = re.compile('a') result = regex.match(s) print(5, result) result = regex.match(s, 15) print (6, result) # print('--search--') result = re.search('a', s) print(7, result) regex = re.compile('a') result =regex.search(s, 15) print(8, result) regex = re.compile('^b') result =regex.search(s,11) print(9, result) result = re.fullmatch ('bag', s) print (10, result) regex = re.compile ('bag') result = regex.fullmatch(s) print (11, result) result = regex. fullmatch(s, 7) print (12, result) result = regex. fullmatch(s, 7, 10) print(13, result) # [7, 10)
2
import re s = '''bottle\nbag\nbig\nable''' for i,c in enumerate(s, 1): print((i-1, c), end='\n' if i%8==0 else ' ') print() result = re.findall('b', s) print(1, result) regex = re.compile('^b') result = regex.findall(s) print(2, result) regex = re.compile('^b', re.M) result = regex.findall(s, 7) print(3, result) regex = re.compile('^b', re.S) result = regex.findall(s) print(4, result) regex = re.compile('^b', re.S) result = regex.findall(s, 7) print(5, result) regex = re.compile('^b', re.M) result = regex.findall(s, 7, 10) print(6, result)
3
import re s = '''bottle\nbag\nbig\nable''' for i,c in enumerate(s, 1): print((i-1, c), end='\n' if i%8==0 else ' ') print() regex = re.compile('b\wg') result = regex.sub('magedu', s) print(1, result) result = regex.sub('magedu', s, 1) print(2, result) regex = re.compile('\s+') result = regex.subn('\t', s) print(6, result)
4
import re s = '''01 bottle 02 bag 03 big 100 able''' for i,c in enumerate(s, 1): print((i-1, c), end='\n' if i%8==0 else ' ') print() print(s.split()) result = re.split('[\s\d]+', s) print(1, result) regex = re.compile('^[\s\d]+') result = regex.split(s) print(2, result) regex = re.compile('^[\s\d]+', re.M) result = regex.split(s) print(3, result) regex = re.compile('\s+\d+\s+') result = regex.split(' '+s) print(4, result) regex = re.compile('\s?\d{2}\d?\s+') result = regex.split(s) result = list(filter(None,result)) print(5, result)
5
import re s = '''bottle\nbag\nbig\nable''' for i,c in enumerate(s, 1): print((i-1, c), end='\n' if i%8==0 else ' ') print() regex = re.compile('(b\w+)') result = regex.match(s) print(result) print(type(result)) print(1, 'match', result.groups()) result = regex.search(s) print(2, 'search', result.groups()) regex = re.compile('(b\w+)\n(?P<name2>b\w+)\n(?P<name3>b\w+)') result = regex.match(s) print (3, 'match', result) print(4, result.group(3), result.group(2), result.group(1)) print (5, result.group(0).encode()) print (6, result.group('name2'), result.group('name3')) print (6, result.groups()) print(7, result.groupdict()) # result = regex.findall(s) # for x in result: # print(type(x), x) regex = re.compile('(?P<head>b\w+)') result = regex.finditer(s) for x in result: print(type(x), x, x.group(), x.group('head'))