python与正则表达式:re模块详解

时间:2022-05-10 22:37:39

re模块是python中处理正在表达式的一个模块

正则表达式知识储备:http://www.cnblogs.com/huamingao/p/6031411.html

 

1. match(pattern, string, flags=0)

从字符串的开头进行匹配, 匹配成功就返回一个匹配对象,匹配失败就返回None

flags的几种值
X 忽略空格和注释

I 忽略大小写的区别   case-insensitive matching

S  . 匹配任意字符,包括新行

python与正则表达式:re模块详解python与正则表达式:re模块详解
def match(pattern, string, flags=0):
"""Try to apply the pattern at the start of the string, returning
a match object, or None if no match was found.
"""
return _compile(pattern, flags).match(string)
match(pattern, string, flags=0)

 

2. search(pattern, string, flags=0)

浏览整个字符串去匹配第一个,未匹配成功返回None

python与正则表达式:re模块详解python与正则表达式:re模块详解
def search(pattern, string, flags=0):
"""Scan through string looking for a match to the pattern, returning
a match object, or None if no match was found.
"""
return _compile(pattern, flags).search(string)
search(pattern, string, flags=0)

 

3. findall(pattern, string, flags=0)

match和search均用于匹配单值,即:只能匹配字符串中的一个,如果想要匹配到字符串中所有符合条件的元素,则需要使用 findall。

findall,获取非重复的匹配列表;如果有一个组则以列表形式返回,且每一个匹配均是字符串;如果模型中有多个组,则以列表形式返回,且每一个匹配均是元祖;空的匹配也会包含在结果中

python与正则表达式:re模块详解python与正则表达式:re模块详解
def findall(pattern, string, flags=0):
"""Return a list of all non-overlapping matches in the string.

If one or more capturing groups are present in the pattern, return
a list of groups; this will be a list of tuples if the pattern
has more than one group.

Empty matches are included in the result.
"""
return _compile(pattern, flags).findall(string)
findall(pattern, string, flags=0)

 

4. sub(pattern,repl,string,count=0,flags=0)

替换匹配成功的指定位置字符串

python与正则表达式:re模块详解python与正则表达式:re模块详解
def sub(pattern, repl, string, count=0, flags=0):
"""Return the string obtained by replacing the leftmost
non-overlapping occurrences of the pattern in string by the
replacement repl. repl can be either a string or a callable;
if a string, backslash escapes in it are processed. If it is
a callable, it's passed the match object and must return
a replacement string to be used.
"""
return _compile(pattern, flags).sub(repl, string, count)
sub(pattern, repl, string, count=0, flags=0)
 
5. split(pattern,string,maxsplit=0,flags=0) 根据正则匹配分割字符串python与正则表达式:re模块详解python与正则表达式:re模块详解
def split(pattern, string, maxsplit=0, flags=0):
"""Split the source string by the occurrences of the pattern,
returning a list containing the resulting substrings. If
capturing parentheses are used in pattern, then the text of all
groups in the pattern are also returned as part of the resulting
list. If maxsplit is nonzero, at most maxsplit splits occur,
and the remainder of the string is returned as the final element
of the list.
"""
return _compile(pattern, flags).split(string, maxsplit)
split(pattern, string, maxsplit=0, flags=0)
 

6.compile()

python代码最终会被编译为字节码,之后才被解释器执行。在模式匹配之前,正在表达式模式必须先被编译成regex对象,预先编译可以提高性能,re.compile()就是用于提供此功能。

 

7. group()与groups() 

匹配对象的两个主要方法:

group()  返回所有匹配对象,或返回某个特定子组,如果没有子组,返回全部匹配对象

groups() 返回一个包含唯一或所有子组的的元组,如果没有子组,返回空元组

 

python与正则表达式:re模块详解python与正则表达式:re模块详解
    def group(self, *args):
"""Return one or more subgroups of the match.

:rtype: T | tuple
"""
pass

def groups(self, default=None):
"""Return a tuple containing all the subgroups of the match, from 1 up
to however many groups are in the pattern.

:rtype: tuple
"""
pass
Group() and Groups()