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,返回一个顺序访问 ...
随机推荐
- 配置 VirtualBox backend - 每天5分钟玩转 Docker 容器技术(75)
Rexy-Ray 支持多种 backend,上一节我们已经安装配置了 Rex-Ray,今天演示如何配置 VirtualBox backend. 在 VirtualBox 宿主机,即我的笔记本上启动 v ...
- android-蓝牙通信
一:简介 由于项目曾经想用蓝牙通信,但由于蓝牙传输速度比较慢,最终还是没有使用蓝牙,不过还是在空闲之余研究了蓝牙通信,鉴于目前网上蓝牙这块教程并不多,尤其是从蓝牙扫描,蓝牙配对,蓝牙通信等完整的教程更 ...
- Python操作csv文件
1.什么是csv文件 The so-called CSV (Comma Separated Values) format is the most common import and export fo ...
- 屏蔽掉Google Chrome 浏览器 textarea 单词拼写检测
可以使用html5的spellcheck属性来关闭对元素内容进行拼写检查. <!-以下两种书写方法正确--> <textarea spellcheck="true" ...
- JavaScript设计模式接口
JavaScript中实现接口的方法有三种: 第一种,使用注释的方法实现接口 特点:(1)最简单,但是功能最弱(2)利用 interface和 implement"文字"(3)把他 ...
- Python实战之字符串的详细简单练习
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__' ...
- c#使用GDI+简单绘图(二)
// Create the in-memory bitmap where you will draw the image. // This bitmap is 300 pixels wide and ...
- DevOps之网络
唠叨话 关于德语噢屁事的知识点,仅提供专业性的精华汇总,具体知识点细节,参考教程网址,如需帮助,请留言. <网络(Network)> 关于网络的网络架构和网络模型:知识与技能的层次(知道. ...
- Mybatis Sql片段的应用
在一个查询里,针对各种不同数据库,有时候只是一部分 SQL 是不相同的,为避免相同的部分复制多次,所以将不相同的部分进行适当的隔离,再重用就可以了. 在 MyBatis 里声明两段 <sql d ...
- 从给数组中的对象去重看Javascript中的reduce()
假设有这样一个数组: let person = [ {id: 0, name: "小明"}, {id: 1, name: "小张"}, {id: 2, name ...