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 MatchObject instances 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()的用法的更多相关文章

  1. Python: 字符串搜索和匹配,re.compile() 编译正则表达式字符串,然后使用match() , findall() 或者finditer() 等方法

    1. 使用find()方法 >>> text = 'yeah, but no, but yeah, but no, but yeah' >>> text.find( ...

  2. 第11.3节 Python正则表达式搜索支持函数search、match、fullmatch、findall、finditer

    一. 概述 re模块的函数search.match.fullmatch.findall.finditer都是用于搜索文本中是否包含指定模式的串,函数的参数都是一样的,第一个参数是模式串.第二个是搜索文 ...

  3. python正则表达式--findall、finditer方法

    findall方法 相比其他方法,findall方法有些特殊.它的作用是查找字符串中所有能匹配的字符串,并以结果存于列表中,然后返回该列表 注意: match 和 search 是匹配一次 finda ...

  4. Python中re的match、search、findall、finditer区别

    原文地址: http://blog.csdn.net/djskl/article/details/44357389 这四个方法是从某个字符串中寻找特定子串或判断某个字符串是否符合某个模式的常用方法. ...

  5. 【整理】python中re的match、search、findall、finditer区别

    match 从首字母开始开始匹配,string如果包含pattern子串,则匹配成功,返回Match对象,失败则返回None,若要完全匹配,pattern要以$结尾. search 若string中包 ...

  6. python re的findall和finditer

    记录一个现象: 今天在写程序的时候,发现finditer和findall返回的结果不同.一个为list,一个为iterator. 红色箭头的地方,用finditer写的时候,print(item.gr ...

  7. python正则表达式(5)--findall、finditer方法

    findall方法 相比其他方法,findall方法有些特殊.它的作用是查找字符串中所有能匹配的字符串,并以结果存于列表中,然后返回该列表 注意: match 和 search 是匹配一次 finda ...

  8. re正则match、search、findall、finditer函数方法使用

    match 匹配string 开头,成功返回Match object, 失败返回None,只匹配一个. search 在string中进行搜索,成功返回Match object, 失败返回None, ...

  9. python 正则表达式 re.findall &re.finditer

    语法: findall 搜索string,以列表形式返回全部能匹配的子串 re.findall(pattern, string[, flags]) finditer 搜索string,返回一个顺序访问 ...

随机推荐

  1. 集成Mybatis

    本文根据个人喜好记录"腾讯课堂"的<Java项目之Maven+SpringMVC+Spring+Mybatis+MySql消费查询系统>视频教程关键步骤信息,视频地址: ...

  2. Naive and Silly Muggles hdu4720

    Naive and Silly Muggles Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  3. Python多线程练习(threading)

    这几天学习python多线程的时候,试了几次thread模块和threading模块,发现thread模块非常的不好用.强烈不建议大家使用thread,建议使用threading模块,此模块对thre ...

  4. ASP.NET没有魔法——开篇-用VS创建一个ASP.NET Web程序

    为什么写这一系列文章? 本系列文章基于ASP.NET MVC,在ASP.NET Core已经发布2.0版本,微服务漫天的今天为什么还写ASP.NET?. 答:虽然现在已经有ASP.NET Core并且 ...

  5. 不同Activity之间传递线程

    场景:Android由Activiy A创建Activiy B时 ,A创建的线程B中依然需要调用,这时候需要在两个activity之间传递线程的信息. 解决: 方式一:线程自己维护自己的静态句柄(比较 ...

  6. python urllib、urlparse、urllib2、cookielib

    1.urllib模块 1.urllib.urlopen(url[,data[,proxies]]) 打开一个url的方法,返回一个文件对象,然后可以进行类似文件对象的操作.本例试着打开google i ...

  7. ZOJ 2002 Copying Books 二分 贪心

    传送门:Zoj2002 题目大意:从左到右把一排数字k分,得到最小化最大份,如果有多组解,左边的尽量小. 思路:贪心+二分(参考青蛙过河). 方向:从右向左. 注意:有可能最小化时不够k分.如     ...

  8. MVC调用部分视图PartialView

    using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Demo2 ...

  9. Hexo+Github搭建博客

    要使用Hexo,需要在你的系统中支持Nodejs以及Git,如果还没有,那就开始安装吧! 安装Node.js 下载Node.js 参考地址:安装Node.js 安装Git 下载地址:http://gi ...

  10. 在HBulider中如何快速的生成有序(ol)和无序(ul)列表

    首先你需要创建一个HTML文件,然后在body里面写入你要创建的类型(有序或者无序),然后列表的个数个人来定: 格式如下: ul > li * 5    代表我要创建一个列表为5个的无序类型 然 ...