关于正则表达式基本语法请参考之前发过的文章常用正则表达式锦集与Python中正则表达式的用法,正则表达式扩展语法的高级用法后面会专门整理后再发。
Python标准库re提供了正则表达式操作所需要的功能,既可以直接使用re模块中的方法(见下表)来处理字符串。
方法 | 功能说明 |
---|---|
compile(pattern[, flags]) | 创建模式对象 |
escape(string) | 将字符串中所有特殊正则表达式字符转义 |
findall(pattern, string[, flags]) | 列出字符串中模式的所有匹配项 |
finditer(pattern, string, flags=0) | 返回包含所有匹配项的迭代对象,其中每个匹配项都是match对象 |
fullmatch(pattern, string, flags=0) | 尝试把模式作用于整个字符串,返回match对象或None |
match(pattern, string[, flags]) | 从字符串的开始处匹配模式,返回match对象或None |
purge() | 清空正则表达式缓存 |
search(pattern, string[, flags]) | 在整个字符串中寻找模式,返回match对象或None |
split(pattern, string[, maxsplit=0]) | 根据模式匹配项分隔字符串 |
sub(pat, repl, string[, count=0]) | 将字符串中所有pat的匹配项用repl替换,返回新字符串,repl可以是字符串或返回字符串的可调用对象,该可调用对象作用于每个匹配的match对象 |
subn(pat, repl, string[, count=0]) | 将字符串中所有pat的匹配项用repl替换,返回包含新字符串和替换次数的二元元组,repl可以是字符串或返回字符串的可调用对象,该可调用对象作用于每个匹配的match对象 |
其中函数参数“flags”的值可以是re.I(注意是大写字母I,不是数字1,表示忽略大小写)、re.L(支持本地字符集的字符)、re.M(多行匹配模式)、re.S(使元字符“.”匹配任意字符,包括换行符)、re.U(匹配Unicode字符)、re.X(忽略模式中的空格,并可以使用#注释)的不同组合(使用“|”进行组合)。
下面的代码演示了直接使用re模块中的方法和正则表达式处理字符串的用法,其中match()函数用于在字符串开始位置进行匹配,而search()函数用于在整个字符串中进行匹配,这两个函数如果匹配成功则返回match对象,否则返回None。 >>> import re #导入re模块 >>> text = 'alpha. beta....gamma delta' #测试用的字符串 >>> re.split('[\. ]+', text) #使用指定字符作为分隔符进行分隔 ['alpha', 'beta', 'gamma', 'delta'] >>> re.split('[\. ]+', text, maxsplit=2) #最多分隔2次 ['alpha', 'beta', 'gamma delta'] >>> re.split('[\. ]+', text, maxsplit=1) #最多分隔1次 ['alpha', 'beta....gamma delta'] >>> pat = '[a-zA-Z]+' >>> re.findall(pat, text) #查找所有单词 ['alpha', 'beta', 'gamma', 'delta'] >>> pat = '{name}' >>> text = 'Dear {name}...' >>> re.sub(pat, 'Mr.Dong', text) #字符串替换 'Dear Mr.Dong...' >>> s = 'a s d' >>> re.sub('a|s|d', 'good', s) #字符串替换 'good good good' >>> s = "It's a very good good idea" >>> re.sub(r'(\b\w+) \1', r'\1', s) #处理连续的重复单词 "It's a very good idea" >>> re.sub('a', lambda x:x.group(0).upper(), 'aaa abc abde') #repl为可调用对象 'AAA Abc Abde' >>> re.sub('[a-z]', lambda x:x.group(0).upper(), 'aaa abc abde') 'AAA ABC ABDE' >>> re.sub('[a-zA-z]', lambda x:chr(ord(x.group(0))^32), 'aaa aBc abde') #英文字母大小写互换 'AAA AbC ABDE' >>> re.subn('a', 'dfg', 'aaa abc abde') #返回新字符串和替换次数 ('dfgdfgdfg dfgbc dfgbde', 5) >>> re.sub('a', 'dfg', 'aaa abc abde') 'dfgdfgdfg dfgbc dfgbde' >>> re.escape('http://www.python.org') #字符串转义 'http\\:\\/\\/www\\.python\\.org' >>> print(re.match('done|quit', 'done'))#匹配成功,返回match对象 <_sre.SRE_Match object at 0x00B121A8> >>> print(re.match('done|quit', 'done!')) #匹配成功 <_sre.SRE_Match object at 0x00B121A8> >>> print(re.match('done|quit', 'doe!')) #匹配不成功,返回空值None None >>> print(re.match('done|quit', 'd!one!')) #匹配不成功 None >>> print(re.search('done|quit', 'd!one!done')) #匹配成功 <_sre.SRE_Match object at 0x0000000002D03D98>
下面的代码使用不同的方法删除字符串中多余的空格,如果遇到连续多个空格则只保留一个,同时删除字符串两侧的所有空白字符。 >>> import re >>> s = 'aaa bb c d e fff ' >>> ' '.join(s.split()) #直接使用字符串对象的方法 'aaa bb c d e fff' >>> ' '.join(re.split('[\s]+', s.strip())) #同时使用re模块中的函数和字符串对象的方法 'aaa bb c d e fff' >>> ' '.join(re.split('\s+', s.strip())) #与上一行代码等价 'aaa bb c d e fff' >>> re.sub('\s+', ' ', s.strip()) #直接使用re模块的字符串替换方法 'aaa bb c d e fff'
下面的代码使用几种不同的方法来删除字符串中指定内容: >>> email = "tony@tiremove_thisger.net" >>> m = re.search("remove_this", email) #使用search()方法返回的match对象 >>> email[:m.start()] + email[m.end():] #字符串切片 'tony@tiger.net' >>> re.sub('remove_this', '', email) #直接使用re模块的sub()方法 'tony@tiger.net' >>> email.replace('remove_this', '') #直接使用字符串替换方法 'tony@tiger.net'
下面的代码使用以“\”开头的元字符来实现字符串的特定搜索。 >>> import re >>> example = 'Beautiful is better than ugly.' >>> re.findall('\\bb.+?\\b', example) #以字母b开头的完整单词,此处问号?表示非贪心模式 ['better'] >>> re.findall('\\bb.+\\b', example) #贪心模式的匹配结果 ['better than ugly'] >>> re.findall('\\bb\w*\\b', example) ['better'] >>> re.findall('\\Bh.+?\\b', example) #不以h开头且含有h字母的单词剩余部分 ['han'] >>> re.findall('\\b\w.+?\\b', example) #所有单词 ['Beautiful', 'is', 'better', 'than', 'ugly'] >>> re.findall('\w+', example) #所有单词 ['Beautiful', 'is', 'better', 'than', 'ugly'] >>> re.findall(r'\b\w.+?\b', example) #使用原始字符串 ['Beautiful', 'is', 'better', 'than', 'ugly'] >>> re.split('\s', example) #使用任何空白字符分隔字符串 ['Beautiful', 'is', 'better', 'than', 'ugly.'] >>> re.findall('\d+\.\d+\.\d+', 'Python 2.7.13') #查找并返回x.x.x形式的数字 ['2.7.13'] >>> re.findall('\d+\.\d+\.\d+', 'Python 2.7.13,Python 3.6.0') ['2.7.13', '3.6.0'] >>> s = '<html><head>This is head.</head><body>This is body.</body></html>' >>> pattern = r'<html><head>(.+)</head><body>(.+)</body></html>' >>> result = re.search(pattern, s) >>> result.group(1) #第一个子模式 'This is head.' >>> result.group(2) #第二个子模式 'This is body.'