zoukankan      html  css  js  c++  java
  • python正则表达式 Python Re模块

     

    最近在学python 练习的时候随手写的,方便以后自己参考~如果能对其他同学有所帮助就再好不过了

    希望大家指正哦~  我会随时整理的,先这样~

    正则表达式

    •  1.元字符([ ]),它用来指定一个character class。所谓character classes就是你想要匹配的字符(character)的集合.字符(character)可以单个的列出,也可以通过"-"来分隔两个字符来表示一个范围。例如,[abc]匹配a,b或者c当中任意一个字符,[abc]也可以用字符区间来表示---[a-c].如果想要匹配单个大写字母,你可以用[A-Z]。
    •      元字符(metacharacters)在character class里面不起作用,如[akm$]将匹配"a","k","m","$"中的任意一个字符。在这里元字符(metacharacter)"$"就是一个普通字符。
    •      2.元字符[^]. 你可以用补集来匹配不在区间范围内的字符。其做法是把"^"作为类别的首个字符;其它地方的"^"只会简单匹配 "^"字符本身。例如,[^5] 将匹配除 "5" 之外的任意字符。同时,在[ ]外,元字符^表示匹配字符串的开始,如"^ab+"表示以ab开头的字符串。
    • 20130515113723855

    反斜杠问题

    与大多数编程语言相同,正则表达式里使用””作为转义字符,这就可能造成反斜杠困扰。假如你需要匹配文本中的字符””,

    Python里的原生字符串很好地解决了这个问题,这个例子中的正则表达式可以使用r”\”表示。同样,匹配一个数字的”\d”可以写成r”d”。有了原生字符串,妈妈也不用担心是不是漏写了反斜杠,写出来的表达式也更直观勒。

    获得这个匹配模式:需要利用re.compile方法就可以

    __author__ = 'CQC'
    # -*- coding: utf-8 -*-
    
    #导入re模块
    import re
    
    # 将正则表达式编译成Pattern对象,注意hello前面的r的意思是“原生字符串”
    pattern = re.compile(r'hello')
    
    # 使用re.match匹配文本,获得匹配结果,无法匹配时将返回None
    result1 = re.match(pattern,'hello')
    result2 = re.match(pattern,'helloo CQC!')
    result3 = re.match(pattern,'helo CQC!')
    result4 = re.match(pattern,'hello CQC!')
    
    #如果1匹配成功
    if result1:
        # 使用Match获得分组信息
        print result1.group()//默认为组0
    else:
        print '1匹配失败!'
    
    
    #如果2匹配成功
    if result2:
        # 使用Match获得分组信息
        print result2.group()
    else:
        print '2匹配失败!'
    
    
    #如果3匹配成功
    if result3:
        # 使用Match获得分组信息
        print result3.group()
    else:
        print '3匹配失败!'
    
    #如果4匹配成功
    if result4:
        # 使用Match获得分组信息
        print result4.group()
    else:
        print '4匹配失败!'
    

    属性:
    1.string: 匹配时使用的文本  是原来的文本~

    2.re: 匹配时使用的Pattern对象。

    4.endpos: 文本中正则表达式结束搜索的索引。值与Pattern.match()和Pattern.seach()方法的同名参数相同。
    5.lastindex: 最后一个被捕获的分组在文本中的索引。如果没有被捕获的分组,将为None。

    # 匹配如下内容:单词+空格+单词+任意字符

    m = re.match(r'(w+) (w+)(?P<sign>.*)', 'hello world!')
    三个分组
    w+所有字母  hello
    2  w+所有字母 world 
    3 分组名字为sign  内容为任意字符的*所有(集合)

    match()函数只检测re是不是在string的开始位置匹配,search()会扫描整个string查找匹配,

    pattern = re.compile(r'world')

    match = re.match(pattern,'hello world!')
    if match:
    # 使用Match获得分组信息
    print match.group()
    else:print "NO"
    ### 输出 ###
    # NO


    match = re.search(pattern,'hello world!')
    if match:
    # 使用Match获得分组信息
    print match.group()
    else:print "NO"
    import re
    #输出world

     (3)re.split(pattern, string[, maxsplit])

    以数字(可以多个数字)为分割 ,maxsplit=2 表示分割两次(不是两部分)指的是用“数字”分割2次(这里有4个数字)

    pattern = re.compile(r'd+')
    print re.split(pattern,'one1two2three3four4')
    
    ### 输出 ###
    # ['one', 'two', 'three', 'four', '']
    
    
    
    pattern = re.compile(r'd+')
    print re.split(pattern,'one1two2three3four4',2)
    
    ### 输出 ###
    # ['one', 'two', 'threefour']
    re.split(pattern, string[, maxsplit])

    (4)re.findall(pattern, string[, flags])见名思议 find all match 找出所有匹配的,这个应该是很常用的语句吧~

    1 pattern = re.compile(r'W+')
    2 print re.findall(pattern, '/one1two*2three3four4!')
    3 
    4 ### 输出 ###
    5 # ['/', '*', '!']
    搜索string,返回一个顺序访问每一个匹配结果

    w:代表字母数字下划线

      

    (6)re.sub(pattern, repl, string[, count])

    对匹配的文本 用repl 替换  这里的2(id)代表的是第二个分组   0分组是整个文本!!!第一个左括号是第一个分组

    pattern = re.compile(r'(w+) (w+)')
    s = 'i say, hello world!'
    你看这里的第一个分组就是i
    第二个分组是say
    print re.sub(pattern,r'2 1', s)
    用say i替换i say

    在这个例子里的 func函数用的挺巧妙

    .title是使得第一字母大写?~~?!

    import re
    
    pattern = re.compile(r'(w+) (w+)')
    s = 'i say, hello world!'
    
    print re.sub(pattern,r'2 1', s)
    
    def func(m):
        return m.group(1).title() + ' ' + m.group(2).title()
    
    print re.sub(pattern,func, s)
    
    ### output ###
    # say i, world hello!
    # I Say, Hello World!
    

      

    re.subn(pattern, repl, string[, count])

    只是输出结果中有一个替换次数

    输出结果对比

    say i, world hello!
    I Say, Hello World!
    ('say i, world hello!', 2)
    ('I Say, Hello World!', 2)


    SET(不重复)

    s = set(['Adam', 'Lisa', 'Bart', 'Paul']) #创建 set 的方式是调用 set() 并传入一个 listlist的元素将作为set的元素
    s是set集合创建的一个对象

    s = set(['Adam', 'Lisa', 'Bart', 'Paul'])
    print 'Bart' in s

    输出:true

    可以用“in”来判

  • 相关阅读:
    What is the difference between the ways to implement inheritance in javascript.
    understand dojo/domReady!
    Using dijit/Destroyable to build safe Components
    Session Tracking Approaches
    difference between forward and sendredirect
    What is the DD in java web application
    棋牌游戏-后端架构(1)
    成为技术领导者笔记--领导的MOI模型
    最小表示法
    SGI STL rope
  • 原文地址:https://www.cnblogs.com/ldphoebe/p/5564035.html
Copyright © 2011-2022 走看看