Python 正则re模块之compile()和findall()详解
来源:互联网 发布:钓鱼天气软件哪款好 编辑:程序博客网 时间:2024/06/05 19:36
首先我们看下官方文档里关于的compile的说明:
re.compile(pattern, flags=0)Compile a regular expression pattern into a regular expression object, which can be used for matching using its match() and search() methods, described below.The expression’s behaviour can be modified by specifying a flags value. Values can be any of the following variables, combined using bitwise OR (the | operator).
</pre><pre name="code" class="python">The sequence:prog = re.compile(pattern)result = prog.match(string)<strong><span style="font-size:24px;">is equivalent to</span></strong>result = re.match(pattern, string)but using re.compile() and saving the resulting regular expression object for reuse is more efficient when the expression will be used several times in a single program.Note:The compiled versions of the most recent patterns passed to re.compile() and the module-level matching functions are cached, so programs that use only a few regular expressions at a time needn’t worry about compiling regular expressions.
下面是flag dotall的说明:
re.DOTALLMake the '.' special character match any character at all, including a newline; without this flag, '.' will match anything except a newline.
》》》》》》》》》》》》》》》》》》》》
下面是关于findall的说明:
re.findall(pattern, string, flags=0)Return all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found. 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 unless they touch the beginning of another match.
》》》》》》》》》》》》》》》》》》》》
下面举个栗子进行讲解:>>> import re>>> s = "adfad asdfasdf asdfas asdfawef asd adsfas ">>> reObj1 = re.compile('((\w+)\s+\w+)')>>> reObj1.findall(s)[('adfad asdfasdf', 'adfad'), ('asdfas asdfawef', 'asdfas'), ('asd adsfas', 'asd')]>>> reObj2 = re.compile('(\w+)\s+\w+')>>> reObj2.findall(s)['adfad', 'asdfas', 'asd']>>> reObj3 = re.compile('\w+\s+\w+')>>> reObj3.findall(s)['adfad asdfasdf', 'asdfas asdfawef', 'asd adsfas']
代码参考下图进行理解:
findall函数返回的总是正则表达式在字符串中所有匹配结果的列表list,此处主要讨论列表中“结果”的展现方式,即findall中返回列表中每个元素包含的信息。
1.当给出的正则表达式中带有多个括号时,列表的元素为多个字符串组成的tuple,tuple中字符串个数与括号对数相同,字符串内容与每个括号内的正则表达式相对应,并且排放顺序是按括号出现的顺序。
2.当给出的正则表达式中带有一个括号时,列表的元素为字符串,此字符串的内容与括号中的正则表达式相对应(不是整个正则表达式的匹配内容)。
3.当给出的正则表达式中不带括号时,列表的元素为字符串,此字符串为整个正则表达式匹配的内容。
《《《《《《《《《《《《《《《《《
对于.re.compile.findall(data)之后的数据,我们可以通过list的offset索引或者str.join()函数来使之变成str字符串,从而进行方便的处理,下面是python3.5中str.join()的文档:str.join(iterable)Return a string which is the concatenation of the strings in the iterable iterable. A TypeError will be raised if there are any non-string values in iterable, including bytes objects.The separator between elements is the string providing this method.
经过上面的介绍,相信对crawler里的正则有很大的帮助
2 1
- Python 正则re模块之compile()和findall()详解
- Python中正则表达式re模块-compile()和findall()
- python re.compile()和findall()
- python 正则表达式 之re.findall
- python re 模块 findall 函数
- python 正则表达式re.findall
- python 正则表达式re.findall
- python error之re模块的findall与match错误
- python re 模块 findall 函数用法简述
- python re 模块 findall 函数用法简述
- python re 模块 findall 函数用法简述
- python re 模块 findall 函数用法简述
- python re 模块 findall 函数用法简述
- python re 模块 findall 函数用法简述
- python re 模块 findall 函数用法简述
- re模块 match serach findall 详解
- Python之re模块详解
- Python爬虫正则表达式和re模块系列之七
- JS 判断浏览器类型及版本,做浏览器兼容
- Oracle 11g 数据库(64 bit)的安装&卸载(PL/SQL(32 bit))
- oracle备份数据库
- iphone开源项目list
- 【面试笔试算法】Problem 9: 腾讯2016年研发实习笔试题:最长回文子串
- Python 正则re模块之compile()和findall()详解
- EventBus框架源码分析
- Html标签中thead、tbody、tfoot的作用
- MySql入门
- 微博主页面布局思路
- 报错Binary XML file line # : Error inflating class
- (视图)初步学习
- HTML学习之表格表单及框架介绍
- jdk7 api