Re.findall() & Re.finditer()的用法
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.
返回字符串里所有不重叠的模式串匹配,以字符串列表的形式出现。字符串从左往右被扫描,匹配按被发现的顺序返回。如果有一个或多个群出现在模式串中,返回一个群列表;如果模式串有多个串,这将是元组的列表。空匹配将被包括在结果里,除非他们触碰到另外一个匹配的开头。
re.finditer(pattern, string, flags=0)-
Return an iterator yielding
MatchObjectinstances over all non-overlapping matches for the RE pattern in string. The string is scanned left-to-right, and matches are returned in the order found. Empty matches are included in the result unless they touch the beginning of another match.返回一个产生匹配对象实体的迭代器,能产生字符串中所有RE模式串的非重叠匹配。字符串被从左向右扫描,匹配按发现顺序返回。空字符串被包括在结果中除非它们触碰到另一个匹配的开头。
flags参数是可选参数。如果向它传递re模块中的宏常量,就会对匹配方式产生对应的影响。
原题:Re.findall() & Re.finditer()
import re vowels = "AEIOUaeiou"
consonants = "QWRTYPSDFGHJKLZXCVBNMqwrtypsdfghjklzxcvbnm" m = re.findall(r"(?<=[%s])([%s]{2,})(?=[%s])"%(consonants, vowels, consonants), input()) if m:
print("\n".join(m))
else:
print("-1")[] 用于表示一个字符集合。 (?=...) 如果 ... 和目前位置接下来的字符串相同,则匹配成功,但是它不消耗字符串(也就是说,其他模式串也可以使用这些字符进行匹配)。这被称为前看断言(lookahead assertion)。 (?<=...) 如果目前位置之前紧邻的字符串等同于 ... ,则匹配成功。这被称为正面后看断言(positive lookbehind assertion)。 {m,n} 表示匹配m到n个之前正则表达式里的字符,这是个贪心版本,它会匹配尽可能多的字符。
import re vowels = "AEIOU"
consonants = "QWRTYPSDFGHJKLZXCVBNM" m = re.findall(r"(?<=[%s])([%s]{2,})(?=[%s])"%(consonants, vowels, consonants), input(), flags=re.I) if m:
print("\n".join(m))
else:
print("-1")如果 flags=re.I ,表示正则表达式忽略字符的大小写区别。因此代码可做如上修改。
Re.findall() & Re.finditer()的用法的更多相关文章
- Python: 字符串搜索和匹配,re.compile() 编译正则表达式字符串,然后使用match() , findall() 或者finditer() 等方法
1. 使用find()方法 >>> text = 'yeah, but no, but yeah, but no, but yeah' >>> text.find( ...
- 第11.3节 Python正则表达式搜索支持函数search、match、fullmatch、findall、finditer
一. 概述 re模块的函数search.match.fullmatch.findall.finditer都是用于搜索文本中是否包含指定模式的串,函数的参数都是一样的,第一个参数是模式串.第二个是搜索文 ...
- python正则表达式--findall、finditer方法
findall方法 相比其他方法,findall方法有些特殊.它的作用是查找字符串中所有能匹配的字符串,并以结果存于列表中,然后返回该列表 注意: match 和 search 是匹配一次 finda ...
- Python中re的match、search、findall、finditer区别
原文地址: http://blog.csdn.net/djskl/article/details/44357389 这四个方法是从某个字符串中寻找特定子串或判断某个字符串是否符合某个模式的常用方法. ...
- 【整理】python中re的match、search、findall、finditer区别
match 从首字母开始开始匹配,string如果包含pattern子串,则匹配成功,返回Match对象,失败则返回None,若要完全匹配,pattern要以$结尾. search 若string中包 ...
- python re的findall和finditer
记录一个现象: 今天在写程序的时候,发现finditer和findall返回的结果不同.一个为list,一个为iterator. 红色箭头的地方,用finditer写的时候,print(item.gr ...
- python正则表达式(5)--findall、finditer方法
findall方法 相比其他方法,findall方法有些特殊.它的作用是查找字符串中所有能匹配的字符串,并以结果存于列表中,然后返回该列表 注意: match 和 search 是匹配一次 finda ...
- re正则match、search、findall、finditer函数方法使用
match 匹配string 开头,成功返回Match object, 失败返回None,只匹配一个. search 在string中进行搜索,成功返回Match object, 失败返回None, ...
- python 正则表达式 re.findall &re.finditer
语法: findall 搜索string,以列表形式返回全部能匹配的子串 re.findall(pattern, string[, flags]) finditer 搜索string,返回一个顺序访问 ...
随机推荐
- JAVA 反射(1)
getDeclaredField是可以获取一个类的所有字段. getField只能获取类的public 字段.
- Rectangles hdu2461容斥定理
Rectangles Time Limit: 5000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 概率图模型PGM——D map, I map, perfect map
若F分布的每个条件独立性质都反映在A图中,则A图被称为F分布的D map. 若A图表现出的所有条件独立性质都在F分布中满足(与F分布不矛盾),则A图被称为F分布的I map. 弱A图既是F分布的D m ...
- Docker入门之七Dockerfile
Dockerfile是一个文本格式的配置文本,可以使用它来创建自定义的镜像.首先我们可以先看一个dockerfile是什么样子.这里可以有一个网站不错:http://dockerfile.github ...
- CSS中新属性calc()
CSS3的calc()使用 原文: http://www.w3cplus.com/css3/how-to-use-css3-calc-function.html © w3cplus.com calc( ...
- 配置和启动Kubernetes服务
安装etcd服务 下载安装包 wget https://github.com/coreos/etcd/releases/download/v3.1.3/etcd-v3.1.3-linux-amd64. ...
- JAVA提高二:枚举
JDK5.0中有一个非常有用的特性:枚举,这个特性以前在C语言中出现过,后来JDK出现后,开始觉得没有必要,但随着使用JAVA语言的人数增多,发现大家对枚举的需求非常大,于是又加入了此特性,下面我们来 ...
- es6零基础学习之构建脚本(二)
编译器打开你的es6项目 首先:创建我们的第一个脚本,tasks/util/args.js 在文件里面要先引入一个包,处理命令行参数 import yargs from 'yargs'; / ...
- 关于如何绑定Jquery 的scroll事件(兼容浏览器 Wookmark瀑布流插件)
做一个随屏幕滚动的导航条时,发现一个问题: 火狐.谷歌.ie9正常,ie8.7.6页面滚动时,导航条没有反应. 代码如下: $(document).bind("scroll",fu ...
- maven私服 nexus2.x工作目录解读(翻译文档)
安装nexus repository manager oss 或pro版本时,会创建两个目录:一个目录包含运行环境及应用,通常符号链接为nexus:一个目录包含所有的配置和数据,通常为sonatype ...