zoukankan      html  css  js  c++  java
  • 二.re库介绍

    一.re库的主要功能函数

    1.re.search()用法

    2.re.match()的用法

    >>> match=re.match(r'[1-9]d{5}','BIT 100081')
    >>> if match:
        print(match.group(0))
    
        
    >>> match.group(0)
    Traceback (most recent call last):
      File "<pyshell#12>", line 1, in <module>
        match.group(0)
    AttributeError: 'NoneType' object has no attribute 'group'
    
    >>> match=re.match(r'[1-9]d{5}','510000 BIT 100081')
    >>> if match:
        print(match.group(0))
    
        
    510000
    >>> 

    3.re.findall()用法

    >>> match=re.findall(r'[1-9]d{5}','BIT 100081 TSU100084')
    >>> match
    ['100081', '100084']
    >>> 

    4.re.split()用法

    >>> match=re.split(r'[1-9]d{5}','BIT 100081 TSU100084')
    >>> match
    ['BIT ', ' TSU', '']
    >>> match=re.split(r'[1-9]d{5}','BIT 100081 TSU100084',maxsplit=1)
    >>> match
    ['BIT ', ' TSU100084']
    >>> 

    5.re.finditer()用法

    >>> for m in re.finditer(r'[1-9]d{5}','BIT100081 TSU100084'):
        if m:
            print(m.group(0))
    
            
    100081
    100084

    6.re.sub()用法

    >>> re.sub(r'[1-9]d{5}',':zipcode','BIT100081 TUS100084')
    'BIT:zipcode TUS:zipcode'
    >>> 

    二.re库的另一种等价用法

  • 相关阅读:
    kafka 0.8.x producer Example(scala)
    Google V8扩展利器发布:v8-native-binding-generator
    beyond compare秘钥被禁
    STL算法之find
    十条nmap常用的扫描命令
    cgdb UTF-8乱码
    OpenWrt笔记
    openwrt hotplug
    git常用操作
    c99标准的restrict关键字
  • 原文地址:https://www.cnblogs.com/chenshaoping/p/9005540.html
Copyright © 2011-2022 走看看