码迷,mamicode.com
首页 > 其他好文 > 详细

Re.findall() & Re.finditer()的用法

时间:2017-08-25 09:39:51      阅读:487      评论:0      收藏:0      [点我收藏+]

标签:groups   ret   正则表达式   hack   targe   其他   ble   else   com   

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")
View Code

 [] 用于表示一个字符集合。 (?=...) 如果 ... 和目前位置接下来的字符串相同,则匹配成功,但是它不消耗字符串(也就是说,其他模式串也可以使用这些字符进行匹配)。这被称为前看断言(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")
View Code

如果 flags=re.I ,表示正则表达式忽略字符的大小写区别。因此代码可做如上修改。

  

Re.findall() & Re.finditer()的用法

标签:groups   ret   正则表达式   hack   targe   其他   ble   else   com   

原文地址:http://www.cnblogs.com/chenhaoran-the-prophet/p/7426379.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!