zoukankan      html  css  js  c++  java
  • Python re模块

    re.match函数

    re.match 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回none。

    def match(self, string, pos=0, endpos=-1):
    """Matches zero | more characters at the beginning of the string.

    import re
    
    
    num = re.compile(r'(d{4})-(d{7})')
    result = num.match('my number is 0310-5561921')
    print(result)
    
    print('#'*30)
    num_1 = re.compile(r'w*(d{4})-(d{7})')
    result = num_1.match('mynumberis0310-5561921')
    print(result.groups())
    print(result.group(1))
    print(result.group(2))
    
    运行:
    C:Python27python.exe D:/Python/modules/Re.py
    None
    ##############################
    ('0310', '5561921')
    0310
    5561921
    

    re.search方法

    re.search 扫描整个字符串并返回第一个成功的匹配。

    num = re.compile(r'(d{4})-(d{7})')
    
    result_match = num.match(_str)
    result_search = num.search(_str)
    
    print('mache : {0}'.format(result_match))
    print('search : {0}'.format(result_search.group()))
    
    运行:
    C:Python27python.exe D:/Python/modules/Re.py
    mache : None
    search : 0310-5561921
    
    
    _str = 'mynumberis0310-5561921'
    
    num_1 = re.compile(r'w*(d{4})-(d{7})')
    result_match = num_1.match(_str)
    result_search = num_1.search(_str)
    
    # print('match.groups:{0}'.format(result_match.groups()))
    print('match.group(1):{0}'.format(result_match.group(1)))
    print('match.group(2):{0}'.format(result_match.group(2)))
    
    print('search.groups():{0}'.format(result_search.groups()))
    print('search.group(1):{0}'.format(result_search.group(1)))
    print('search.group(2):{0}'.format(result_search.group(2)))
    
    运行:
    match.group(1):0310
    match.group(2):5561921
    search.groups():('0310', '5561921')
    search.group(1):0310
    search.group(2):5561921
    
    

    re.match与re.search的区别

    re.match只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回None;而re.search匹配整个字符串,直到找到一个匹配。

    re.findall函数

    找到 re 匹配的所有子串,并把它们作为一个列表返回。这个匹配是从左到右有序地返回。如果无匹配,返回空列

    _str = 'ha1ha222ha3ha4ha5'
    
    num = re.compile(r'd')
    print(num.findall(_str))
    运行:
    ['1', '2', '2', '2', '3', '4', '5']
    

    re.finditer函数

    找到 re 匹配的所有子串,并把它们作为一个迭代器返回。这个匹配是从左到右有序地返回。如果无匹配,返回空列表

    _str = 'ha1ha222ha3ha4ha5'
    
    num = re.compile(r'd')
    print(num.finditer(_str))
    
    for i in num.finditer(_str):
        print(i)
        print(i.group())
        
    运行:
    <callable-iterator object at 0x00000000037C6DD8>
    <_sre.SRE_Match object at 0x00000000037236B0>
    1
    <_sre.SRE_Match object at 0x0000000003723718>
    2
    <_sre.SRE_Match object at 0x00000000037236B0>
    2
    <_sre.SRE_Match object at 0x0000000003723718>
    2
    <_sre.SRE_Match object at 0x00000000037236B0>
    3
    <_sre.SRE_Match object at 0x0000000003723718>
    4
    <_sre.SRE_Match object at 0x00000000037236B0>
    5
    
    

    re.split函数

    通过正则表达式将字符串分离。如果用括号将正则表达式括起来,那么匹配的字符串也会被列入到list中返回。maxsplit是分离的次数,maxsplit=1分离一次,默认为0,不限制次数。

    _str = 'ha1ha222ha3ha4ha5'
    result_split = re.split(r'd*', _str)
    print(result_split)
    
    运行:
    ['ha', 'ha', 'ha', 'ha', 'ha', '']
    
    
  • 相关阅读:
    ArcGIS超级工具SPTOOLS-制图篇
    ArcGIS超级工具SPTOOLS-MXD操作篇
    ArcGIS超级工具SPTOOLS-影像的批量裁剪和批量合并
    Get Raster Properties获得栅格的信息
    ArcGIS超级工具SPTOOLS-按属性裁剪,矢量数据批量裁剪,矢量数据批量合库
    ArcGIS超级工具SPTOOLS-SHP转数据库,批量数据库转数据库,栅格彩色转黑白
    ArcGIS超级工具SPTOOLS-锐角检查,获得内角并判断是否凸多边形,获得线(面)两个折点方向
    ArcGIS超级工具SPTOOLS-线封闭,点集转面
    ArcGIS超级工具-征地部标准坐标导出导入 SPTOOLS
    arcgis的arcpy写入几何怎么创建一个空心面要素并读取几何和属性信息,根本不够管
  • 原文地址:https://www.cnblogs.com/lijunjiang2015/p/7843531.html
Copyright © 2011-2022 走看看