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. oracle 数据库管理--管理表空间和数据文件

    一.概念表空间是数据库的逻辑组成部分.从物理上讲,数据库数据存放在数据文件中:从逻辑上讲,数据库数据则是存放在表空间中,表空间由一个或多个数据文件组成. 二.数据库的逻辑结构oracle中逻辑结构包括 ...

  2. java踩坑记

    1.String 相等 稍微有点经验的程序员都会用equals比较而不是用 ==,但用equals就真的安全了吗,看下面的代码 user.getName().equals("xiaoming ...

  3. Nginx学习——Nginx简单介绍和Linux环境下的安装

    一:Nginx的简介 百科百科:Nginx Nginx 是一个俄罗斯的哥们开发的,并将其进行了开源. Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器, ...

  4. JAVA对象头

    #为了防止自己忘记,先记着,之前我一直以为<深入理解JAVA虚拟机>写错了来着. 一. JAVA对象 在HotSpot虚拟机中,对象在内存中存储的布局可以分为3块区域:对象头(Header ...

  5. .Neter玩转Linux系列之一:初识Linux

    一.为什么要学习Linux (1)首先我们欣赏一下,曾经的微软是如何看待Linux的,是不是很惊讶,微软还是很可爱的(#^.^#) 如今的微软看待Linux的态度:有人说微软技术那么厉害,难道微软就不 ...

  6. vue环境搭建

    1.Window 上安装Node.js 1.Windows 安装包(.msi) 32 位安装包下载地址 : https://nodejs.org/dist/v4.4.3/node-v4.4.3-x86 ...

  7. 兼容低版本JS的Array.map方法

    前几天去别的公司面试遇到个这样的问题,兼容IE7下的Array.map方法,一脸蒙蔽.后面回来查了下资料发现.Array.map方法是ECMA-262 标准中新添加的方法,在低版本的JS中是木有的. ...

  8. angualrJs清除定时器

    angualrJs清除定时器爬坑之路: 今天发现一个奇怪问题,放在自定义指令里边的定时器竟然在页面跳转之后,在另一个页面这个循环定时器还在执行,这肯定是不行的,会影响系统的性能. 我在angular里 ...

  9. try catch finally 中包含return的几种情况,及返回结果

    当当当,兴致勃勃的第二篇博客,散花~ 下面是正题(敲黑板) 第一种情况:在try和catch中有return,finally中没有return,且finally中没有对try或catch中要 retu ...

  10. pb9常见错误及含义

    1. by zero  发生被0除错误 2. Null object reference  空对象引用 3. Array boundary exceeded  数组越界 4. Enumerated v ...