正则表达式,又称规则表达式。(英语:Regular Expression,在代码中常简写为regex、regexp或RE),计算机科学的一个概念。正则表达式通常被用来检索、替换那些符合某个模式(规则)的文本。 给定一个正则表达式和另一个字符串,我们可以达到如下的目的: 正则表达式的特点是: 在字符串开头匹配 使用
re模块常用方法
给定的字符串是否符合正则表达式的过滤逻辑(称作“匹配”);
可以通过正则表达式,从字符串中获取我们想要的特定部分。
灵活性、逻辑性和功能性非常强;
可以迅速地用极简单的方式达到字符串的复杂控制;
对于刚接触的人来说,比较晦涩难懂。
re模块操作
在Python中通过re模块来完成正则表达式操作match(string[, pos[, endpos]])
string 是待匹配的字符串 pos和 endpos 可选参数,指定字符串的起始和终点位置,默认值分别是 0和 len(字符串长度)。# match 方法:从起始位置开始查找,一次匹配 re.match(pattern, string, flags=0) result = re.match("hello", "hellolzt world") print(result, result.group(), type(result)) pattern,如果匹配成功(可以是空字符串)返回对应的match对象,否则返回None。search 方法
search(string[, pos[, endpos]]) ,string是待匹配的字符串 pos 和 endpos 可选参数,指定字符串的起始和终点位置 。当匹配成功时,返回一个 Match 对象,如果没有匹配上,则返回 None。 扫描整个字符串string,找到与正则表达式pattern的第一个匹配(可以是空字符串),并返回一个对应的match对象。如果没有匹配返回None.re.search(pattern, string, flags=0) result = re.search("hello", "2018hellolzt world") print(result.group()) fullmatch方法
fullmatch(pattern, string, flags=0),是match函数的完全匹配(从字符串开头到结尾)re.fullmatch(pattern, string, flags=0) result = re.fullmatch("hello", "hello1") print(result) string是否整个和pattern匹配,如果是返回对应的match对象,否则返回None。findall方法
findall(string[, pos[, endpos]]),string待匹配的字符串 pos 和 endpos 可选参数,指定字符串的起始和终点位置。findall(pattern, string, flags=0) result = re.findall("hello", "lzt hello china hello world") print(result, type(result)) # 返回列表 split方法
split(string[, maxsplit]),maxsplit用于指定最大分割次数,不指定将全部分割。re.split(pattern, string, maxsplit=0, flags=0) result = re.split("hello", "hello china hello world", 2) print(result, type(result)) # 返回分割列表 sub方法
sub(repl, string[, count]),epl可以是字符串也可以是一个函数:
(1) 如果repl 是字符串,则会使用 repl去替换字符串每一个匹配的子串
(2) 如果repl 是函数,方法只接受一个参数(Match对象),并返回一个字符串用于替换。
(3) count 用于指定最多替换次数,不指定时全部替换。sub(pattern, repl, string, count=0, flags=0) result = re.sub("hello", "hi", "hello china hello world", 2) print(result, type(result)) repl替换pattern匹配到的内容,最多匹配count次iterator方法
finditer(pattern, string, flags=0) result = re.finditer("hello", "hello world hello china") print(result, type(result)) # 返回迭代器 compile方法
compile 函数用于编译正则表达式,生成一个 Pattern 对象compile(pattern, flags=0) pat = re.compile("hello") print(pat, type(pat)) result = pat.search("helloworld") print(result, type(result)) # 编译得到匹配模型 flags
flags作为可选参数,下面列出了常用的几个flag, 它们实际对应的是二进制数,可以通过位或将他们组合使用。flags可能改变正则表达时的行为:
re.I re.IGNORECASE: 匹配中大小写不敏感
re.M re.MULTILINE: “^“匹配字符串开始以及”n"之后;”$“匹配”n"之前以及字符串末尾。通常称为多行模式
re.S re.DOTALL: "."匹配任意字符,包括换行符。通常称为单行模式
如果要同时使用单行模式和多行模式,只需要将函数的可选参数flags设置为re.I| re.S即可。result = re.match("hello", "HeLlo", flags=re.I) print(result) result = re.findall("^abc","abcdenabcd",re.M) print(result) result = re.findall("e$","abcdenabcd",re.M) print(result) result = re.findall(".", "hello n china", flags=re.S) # "." 可以匹配换行符 print(result) result = re.findall(".", "hello n china", flags=re.M) # "." 不可以匹配换行符 print(result)
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算
官方软件产品操作指南 (170)