zoukankan      html  css  js  c++  java
  • python 基础 8.3 match方法和search方法

    一,正则对象的split 方法
    split(string[,maxsplit])
    按照能够匹配的字串讲string 分割后返回列表。maxsplit 用于指定最大分割次数,不指定将全部分割。来查找符合对象的字字符.
     
     
     
    #/usr/bin/python
    #coding=utf-8
    #@Time   :2017/11/18 20:52
    #@Auther :liuzhenchuan
    #@File   :re 的matche 和 seach.py
    import re
     
    print '正则的常用方法'
    a = re.compile(r'd')
    print dir(a)
    print '##'*30 + ' '
     
    >>>
    正则的常用方法
    ['__class__', '__copy__', '__deepcopy__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'findall', 'finditer', 'flags', 'groupindex', 'groups', 'match', 'pattern', 'scanner', 'search', 'split', 'sub', 'subn']
     
     
    #1 match 方法--匹配 .match(string[,pos[,endpos]])
    #string:匹配使用的文本
    #pos:文本中正则比比表达式开始搜索的索引。及开始搜索 string 的下标
    #endpos:文本中正则表达式结束搜索的索引
    #如果不指定破损,默认从开头匹配,如果匹配不到,直接返回None
     
    print '##match 匹配的应用'
    a = 'hello world hello liu'
    #正则匹配除 hello world 与 hello liu
    reg = re.compile(r'((hello w.*)(hello l.*))')
    result = reg.match(a)
    print result.groups()
    print '##'*30+ ' '
    >>>
    ##match 匹配的应用
    ('hello world hello liu', 'hello world ', 'hello liu')
    ############################################################
     
     
     
    print '##match 添加了起始位置和结束位置,match(str,2,)起始位置2,到最后结束'
    b = 'aa' + a
    print '字符串b为:'+ b
    result2 = reg.match(b,2,)
    reg = re.compile(r'((hello w.*)(hello l.*))')
    print result2.groups()
    print '##'*15
     
    >>>
    ##match 添加了起始位置和结束位置,match(str,2,)起始位置2,到最后结束
    字符串b为:aahello world hello liu
    ('hello world hello liu', 'hello world ', 'hello liu')
    ##############################
     
    print '#match写上起始位置与结束位置可以根据字符串b匹配除正则。或者用 w 也可以匹配出'
    reg = re.compile(r'w*((hello w.*)(hello l.*))')
    result3 = reg.match(b)
    print result3.groups()
    print '##'*30
     
    >>>
    #match写上起始位置与结束位置可以根据字符串b匹配除正则。或者用 w 也可以匹配出
    ('hello world hello liu', 'hello world ', 'hello liu')
    ############################################################
     
     
    #正则对象 search() 方法
    这个方法用于查找字符串中可以匹配成功的字串。从string的pos 下标处尝试匹配pattern,如果pattern结束时认可匹配,在返回一个match对象:若无法匹配,则将pos加1后重新尝试匹配,指导pos=endpos 时仍无法匹配范虎None
     
    b = 'aahello world hello liu'
    reg = re.compile(r'((hello w.*)(hello l.*))')
    result4 = reg.search(b)
    print result4
    print result4.groups()
     
    >>>
    <_sre.SRE_Match object at 0x037993E0>
    ('hello world hello liu', 'hello world ', 'hello liu')
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    mysql CREATE USER
    ConvertHelper 通用类
    自定义属性
    为什么建议使用你LocalDateTime,而不是Date?
    使用IDEA插件Alibaba Cloud Toolkit工具一键部署本地应用到ECS服务器
    IDEA-SpringBoot项目设置热部署
    CentOS7中MySQL跨机器数据迁移
    Centos7 使用YUM安装Mariadb
    Linux下svn服务器迁移
    java dateutil工具类Date.add()
  • 原文地址:https://www.cnblogs.com/lzcys8868/p/7858125.html
Copyright © 2011-2022 走看看