re模块常用方法

re模块常用方法

  • 正则表达式,又称规则表达式。(英语:Regular Expression,在代码中常简写为regex、regexp或RE),计算机科学的一个概念。正则表达式通常被用来检索、替换那些符合某个模式(规则)的文本。

  • 给定一个正则表达式和另一个字符串,我们可以达到如下的目的:
    给定的字符串是否符合正则表达式的过滤逻辑(称作“匹配”);
    可以通过正则表达式,从字符串中获取我们想要的特定部分。

  • 正则表达式的特点是:
    灵活性、逻辑性和功能性非常强;
    可以迅速地用极简单的方式达到字符串的复杂控制;
    对于刚接触的人来说,比较晦涩难懂。
    re模块操作
    在Python中通过re模块来完成正则表达式操作

match(string[, pos[, endpos]])
string 是待匹配的字符串 posendpos 可选参数,指定字符串的起始和终点位置,默认值分别是 0len(字符串长度)。

# 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是待匹配的字符串 posendpos 可选参数,指定字符串的起始和终点位置 。当匹配成功时,返回一个 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待匹配的字符串 posendpos 可选参数,指定字符串的起始和终点位置。
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

  • re模块的一些函数中将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","abcde\nabcd",re.M)
print(result)
result = re.findall("e$","abcde\nabcd",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)

猜你喜欢

转载自blog.csdn.net/qq_40679091/article/details/109250279