前言
字符串处理是编程中常用到的操作,本系列总结的目标是通过系统的介绍不同的方法来完成不同复杂度的字符串处理操作。旨在方便大家遇到不同的需求时,可以快速找到合适的处理方式,从而使代码开发快速,简洁,稳定的目的。
Python中的文本处理(三)2种更加pythonic的字符串处理方式
本文为系列第二篇,一些复杂字符串处理. 一些复杂的处理功能的时候,str模块无法满足需求,需要引入文本处理的神器正则表达式(如需系统了解正则表达式,可阅读此文)。本文将介绍一下Python 中re模块的常用方法
1. 匹配
re模块的提供了4种方法
1.1 re.match
从第一个字符开始匹配表达式,如果匹配,返回一个match对象;匹配失败返回None;匹配成功或失败后均不再匹配后面的字符。
1.2 re.fullmatch
将整个字符串和表达式pattern进行匹配,如果匹配,返回一个match对象;匹配失败返回None;
1.3 re.search
从左到右扫描目标字符串,以查找匹配正则表达式的第一个位置,如果找到匹配,返回一个match对象;不继续匹配后续的字符;没有字符串匹配表达式,返回None。
1.4 re.findall
从左到右扫描目标字符串, 查找所有匹配正则表达式的字串,并返回一个列表
re.finditer 匹配方式同re.findall,返回一个iterator
2. 修改
2.1 re.split(pattern, string, maxsplit=0, flags=0)
re.split 是 str.split的增强版,也是将字符串切割,返回一个列表。支持使用正则表达式切割。
# 使用' ', '.', '-', '_'中的任意一个进行切割
In : re.split('[\s._-]', 'This-is my_string, not yours.')
Out: ['This', 'is', 'my', 'string,', 'not', 'yours', '']
# 使用 'is' 或 'not' 单词来进行分割
In : re.split('is|not', 'This-is my_string, not yours.')
Out: ['Th', '-', ' my_string, ', ' yours.']
# 捕获分割符
In : re.split('(is|not)', 'This-is my_string, not yours.')
Out: ['Th', 'is', '-', 'is', ' my_string, ', 'not', ' yours.']
2.2 re.sub
re.sub 是 str.replace的增强版,对字符串进行替换,使用正则表达式匹配待替换字符。
re.subn 功能和re.sub相同,区别是,re.subn返回一个tuple, (new_string, number),number为替换发生的次数
# 将 'is'或'not'替换成 'miao'
In : re.sub('is|not', 'miao', 'This-is my_string, not yours.')
Out: 'Thmiao-miao my_string, miao yours.'
# 将数字替换成'*'
In : re.sub('\d', '*','my password: 09458327')
Out: 'my password: ********'
In : re.subn('\d', '*','my password: 09458327')
Out: ('my password: ********', 8)
3.编译
3.1 re.compile
将正则表达式模式编译成正则表达式对象,可以使用它的match()、search()等方法进行匹配。
In : scheme = re.compile('^https?://')
In : scheme.match('https://www.mm.com')
Out: <re.Match object; span=(0, 8), match='https://'>
In : scheme.match('http://www.mm.com')
Out: <re.Match object; span=(0, 7), match='http://'>
In : scheme.sub('HTTP://', 'http://www.mm.com')
Out: 'HTTP://www.mm.com'
👩:re.purge 是做什么的,什么时候使用?
👨 :当直接使用顶级
re.*
函数而不是re.compile(pattern)
时,缓存用于存储compiled regular expression objects。例如,如果您在循环中使用re.search(r'<some pattern>', string_value)
,那么re
模块将只编译一次'<some pattern>'
并将其存储在缓存中,从而避免了每次都要重新编译模式的情况。re.purge的作用是清除 编译过的表达式缓存,注意缓存也被自动管理,因此通常根本不需要清除。所以不需要调用purge方法。
4. flag汇总
re.A | 使 \w、\W、\b、\B、\d、\D、\s 和 \S 执行仅 ASCII 匹配而不是完整的 Unicode 匹配。 |
re.ASCII | |
re.I | 执行不区分大小写的匹配; 像 [A-Z] 这样的表达式也会匹配小写字母。 |
re.IGNORECASE | |
re.L | 使 \w、\W、\b、\B 和不区分大小写的匹配依赖于当前的语言环境。 |
re.Locale | |
re.M | 指定时,模式字符 '^' 匹配字符串的开头和每行的开头 |
re.MULTILINE | |
re.S | 使'.' 特殊字符完全匹配任何字符,包括换行符。 |
re.DOTALL |
# 忽略大小写
In : re.findall('http', 'http and uppercase HTTP')
Out: ['http']
In : re.findall('http', 'http and uppercase HTTP', re.I)
Out: ['http', 'HTTP']
# 使'^'匹配多行的开头
In : re.findall('^http', 'http and uppercase.\nhttp in 2nd line\nhttp another new line')
Out: ['http']
In : re.findall('^http', 'http and uppercase.\nhttp in 2nd line\nhttp another new line', re.M)
Out: ['http', 'http', 'http']
# re.S 使'.'匹配包括换行符
In : re.match('.*', 'http and uppercase.\nhttp in 2nd line\nhttp another new line')
Out: <re.Match object; span=(0, 19), match='http and uppercase.'>
In : re.match('.*', 'http and uppercase.\nhttp in 2nd line\nhttp another new line', re.S)
Out: <re.Match object; span=(0, 58), match='http and uppercase.\nhttp in 2nd line\nhttp anoth>
4. 总结
re 利用正则表达式进行匹配,可以进行一些复杂的动作。
常用方法: