ASCII, A #使\w,\b,\W,\B,\s,\S只匹配ASCII字符,而不匹配Unicode
DOTALL, S #使字符'.'匹配任意字符,包括换行符
INGORECASE, I #使匹配对大小不敏感
LOCALE, L #做本地化识别 ??
MULTILINE, M #多行匹配,会影响 ??
VERBOSE, V #能够使用正则表达式的verbose装填,使之更加清晰易懂 ??
import re
p = re.compile(r'\W+')
p.split('This is a test string of split().')
['This', 'is', 'a', 'test', 'string', 'of', 'split', '']
p.split('This is a test string of split().', 3)
['This', 'is', 'a', 'test string of split().']
有时我们对于分隔符也比较感兴趣,这时使用捕获括号,就能将分隔符作为列表的元素返回
import re
p = re.compile(r'(\W+)')
p.split('This is a test string of split().')
['This', ' ', 'is', ' ', 'a', ' ', 'test', ' ', 'string', ' ', 'of', ' ', 'split', '().', '']
模块中的函数 re.split(REs, string, [maxstep = 0] )
import re
re.split('[\W]+','This is a test string of split().')
['This', 'is', 'a', 'test', 'string', 'of', 'split', '']
re.split('([\W]+)','This is a test string of split().')
['This', ' ', 'is', ' ', 'a', ' ', 'test', ' ', 'string', ' ', 'of', ' ', 'split', '().', '']
re.split('[\W]+','This is a test string of split().',3)
['This', 'is', 'a', 'test string of split().']
2.替换 sub() 和subn()
正则表达式中的方法
import re
p = re.compile('(blue|white|red)')
p.sub('color','blue socks and red shoes')
'color socks and color shoes'
p.sub('color','blue socks and red shoes',count = 1)
'color socks and red shoes'
subn()的作用类似,但是会返回一个包括新字符串和替换次数的元组
import re
p = re.compile('(blue|white|red)')
p.subn('color','blue socks and red shoes')
('color socks and color shoes', 2)
p.sub('color','no color at all')
('no colors at all', 0)
空匹配只有在紧邻的前面一个没有被替换时才会替换
import re
p = re.compile('x*')
p.sub('-','abxd')
'-a-b-d-'