码迷,mamicode.com
首页 > 编程语言 > 详细

python基础之re模块

时间:2017-04-27 21:08:30      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:正则表达式   通过   open   alt   blog   isp   img   bsp   引擎   

就其本质而言,正则表达式(或 RE)是一种小型的、高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现。正则表达式模式被编译成一系列的字节码,然后由用 C 编写的匹配引擎执行。

字符匹配(普通字符,元字符):

1 普通字符:大多数字符和字母都会和自身匹配
              >>> re.findall(‘alvin‘,‘yuanaleSxalexwupeiqi‘)
                      [‘alvin‘] 

2 元字符:. ^ $ * + ? { } [ ] | ( ) \  #共11个元字符

技术分享
def findall(pattern, string, flags=0):
    """Return a list of all non-overlapping matches in the string.

    If one or more capturing 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."""
    return _compile(pattern, flags).findall(string)
findall剑谱

re.findall(pattern,string) #找到所有的匹配元素,返回列表

(1) . : 匹配除\n以外的任意符号
print(re.findall("a.+d","abcd"))

 

python基础之re模块

标签:正则表达式   通过   open   alt   blog   isp   img   bsp   引擎   

原文地址:http://www.cnblogs.com/luchuangao/p/6776384.html

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