python的正则表达式re模板

来源:互联网 发布:核聚变进展知乎 编辑:程序博客网 时间:2024/06/06 02:08

一,什么是re
Python 的 re 模块(Regular Expression 正则表达式)提供各种正则表达式的匹配操作,使用这一内嵌于 Python 的语言工具,尽管不能满足所有复杂的匹配情况,但足够在绝大多数情况下能够有效地实现对复杂字符串的分析并提取出相关信息。Python 会将正则表达式转化为字节码,利用 C 语言的匹配引擎进行深度优先的匹配。
二,正则表达式语法
如一下图表所示
这里写图片描述
这里写图片描述
这里写图片描述
一些特殊的用法如下
这里写图片描述
这里写图片描述

三,re的主要功能
Python 的 re 正则表达式模块定义了一系列函数,常量以及异常;同时,正则表达式被编译成‘ RegexObject ’实例,本身可以为不同的操作提供方法。接下来简要介绍一下这些函数的功能和用法。
1,re中所包含的属性

>>> import re>>> dir(re)['DEBUG', 'DOTALL', 'I', 'IGNORECASE', 'L', 'LOCALE', 'M', 'MULTILINE', 'S', 'Scanner', 'T', 'TEMPLATE', 'U', 'UNICODE', 'VERBOSE', 'X', '_MAXCACHE', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__version__', '_alphanum', '_cache', '_cache_repl', '_compile', '_compile_repl', '_expand', '_pattern_type', '_pickle', '_subx', 'compile', 'copy_reg', 'error', 'escape', 'findall', 'finditer', 'match', 'purge', 'search', 'split', 'sre_compile', 'sre_parse', 'sub', 'subn', 'sys', 'template']

2,主要属性的解释
2.1 compile函数
python help中的解释

Help on function compile in module re:compile(pattern, flags=0)    Compile a regular expression pattern, returning a pattern object.

re.compile(pattern[, flags])
把正则表达式的模式和标识转化成正则表达式对象,供 match() 和 search() 这两个函数使用。
flags 的取值如下:

re.I 忽略大小写re.L 表示特殊字符集 \w, \W, \b, \B, \s, \S 依赖于当前环境re.M 多行模式re.S 即为’ . ’并且包括换行符在内的任意字符(’ . ’不包括换行符)re.U 表示特殊字符集 \w, \W, \b, \B, \d, \D, \s, \S 依赖于 Unicode 字符属性数据库re.X 为了增加可读性,忽略空格和’ # ’后面的注释

案例:

>>> pattern ='ab*\d?'>>> my_pattern = re.compile(pattern,re.S)

2.2,search函数
python help解释

Help on function search in module re: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.

通过串寻找一个匹配的模式,返回一个匹配的对象,如果没有找到匹配
就返回None
案例:

>>> re.search(my_pattern,'ab23cd')<_sre.SRE_Match object at 0xb714e9f8>>>> re.search(my_pattern,'23cd')>>> >>> m=re.search(my_pattern,'ab23cd')>>> m.group(0)'ab2'>>> mm=re.search(my_pattern,'23cd')>>> mm.group(0)Traceback (most recent call last):  File "<stdin>", line 1, in <module>AttributeError: 'NoneType' object has no attribute 'group'

2.3,match
python help中的解释

Help on function match in module re: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.

match() 函数只在字符串的开始位置尝试匹配正则表达式,也就是只报告从位置 0 开始的匹配情况,而 search() 函数是扫描整个字符串来查找匹配。如果想要搜索整个字符串来寻找匹配,应当用 search()。
案例:

>>> m = re.match(r"ni*",'niiiiiiiihsomrf')>>> m.group(0)'niiiiiiii'>>> m = re.match(r"ni*?",'niiiiiiiisomrf')>>> m.group(0)'n'>>> 

2.4,spilt
python help的解释
Help on function split in module re:

split(pattern, string, maxsplit=0, flags=0)    Split the source string by the occurrences of the pattern, returning a list containing the resulting substrings.

在匹配正则表达式处分割源串并且把分割的串以list形式返回
案例:

>>> re.split(r'a*\d+','a333defa32ade3a24')['', 'def', 'ade', '', '']

2.5,findall
python help解释

Help on function findall in module re:findall(pattern, string, flags=0)    Return a list of all non-overlapping matches in the string.      If one or more 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.

在字符串中找到正则表达式所匹配的所有子串,并组成一个列表返回
案例:

>>> re.findall(r'^ab+/w\d*?','ab12frdabd12ab23adl3')[]>>> re.findall(r'^ab+\d*?','ab12frdabd12ab23adl3')['ab']>>> re.findall(r'^ab\d*?','ab12frdabd12ab23adl3')['ab']>>> re.findall(r'ab\d*?','ab12frdabd12ab23adl3')['ab', 'ab', 'ab']>>> re.findall(r'ab\d*','ab12frdabd12ab23adl3')['ab12', 'ab', 'ab23']

2.6 finditer
python help的解释

Help on function finditer in module re:finditer(pattern, string, flags=0)    Return an iterator over all non-overlapping matches in the    string.  For each match, the iterator returns a match object.    Empty matches are included in the result.

和 findall 类似,在字符串中找到正则表达式所匹配的所有子串,并组成一个迭代器返回。

2.7 sub
python help的解释

Help on function sub in module re: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.

在字符串 string 中找到匹配正则表达式 pattern 的所有子串,用另一个字符串 repl 进行替换。如果没有找到匹配 pattern 的串,则返回未被修改的 string。Repl 既可以是字符串也可以是一个函数。

2.8 subn 和sub 功能相似

可以参考此处https://www.ibm.com/developerworks/cn/opensource/os-cn-pythonre/

1 0
原创粉丝点击