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. [js高手之路] html5 canvas系列教程 - arcTo(弧度与二次,三次贝塞尔曲线以及在线工具)

    之前,我写了一个arc函数的用法:[js高手之路] html5 canvas系列教程 - arc绘制曲线图形(曲线,弧线,圆形). arcTo: cxt.arcTo( cx, cy, x2, y2, ...

  2. 微服务~Eureka实现的服务注册与发现及服务之间的调用

    微服务里一个重要的概念就是服务注册与发现技术,当你有一个新的服务运行后,我们的服务中心可以感知你,然后把加添加到服务列表里,然后当你死掉后,会从服务中心把你移除,而你作为一个服务,对其它服务公开的只是 ...

  3. Instance of 的用法

    来自:百度百科 instanceof主要用于判断是否是某个类的实例 任何的对象都可以调用 返回结果是Boolean型数值Class AA a=new A();boolean b=a instanceo ...

  4. 18.Llinux-触摸屏驱动(详解)

    本节的触摸屏驱动也是使用之前的输入子系统 1.先来回忆之前第12节分析的输入子系统 其中输入子系统层次如下图所示, 其中事件处理层的函数都是通过input_register_handler()函数注册 ...

  5. Clojure——学习迷宫生成

    背景 初学clojure,想着看一些算法来熟悉clojure语法及相关算法实现. 找到一个各种语言生成迷宫的网站:http://rosettacode.org/wiki/Maze_generation ...

  6. windows下怎么解决Python双版本问题

    相信大家会在windows下会遇到Python双版本问题 当我们装了Python2和Python3时我们好只能在命令栏调出最高版本的那个低版本的难道消失了吗?今天我们就解决这个问题! 1.下载 我们在 ...

  7. 【转】HTTP Header 详解

    HTTP(HyperTextTransferProtocol)即超文本传输协议,目前网页传输的的通用协议.HTTP协议采用了请求/响应模型,浏览器或其他客户端发出请求,服务器给与响应.就整个网络资源传 ...

  8. java web mysql 入门知识讲解

     MySQL学习笔记总结 一.SQL概述: SQL:Structured Query Language的缩写(结构化查询语言) SQL工业标准:由ANSI(ISO核心成员) 按照工业标准编写的SQ ...

  9. Winform退出运行后,删除运行目录(批处理方法)

    /// <summary> /// Winform程序退出删除运行目录 FormClosed调用 /// </summary> private void DeletExeFil ...

  10. 详解面向对象编程——JavaScriptOOP

        前  言 絮叨絮叨 学习了JS之后,不知道大家觉得怎们样呢? 今天我们就来讲一下JS中最重要的一个环节,JavaScript中的面向对象编程OOP,这里的东西有点难,也有点绕. 可是! 不要灰 ...