zoukankan      html  css  js  c++  java
  • Python正则表达式(4)---字符串匹配分组

    • Python正则表达式(4)---字符串匹配分组
      •  
        字符 功能
        | 匹配左右任意一个表达式(或)
        (ab) 将括号中字符作为一个分组(与)
        um 引用分组num匹配到的字符串
        (?P<name>) 分组起别名
        (?P=name) 引用别名为name分组匹配到的字符串
         
         
         
         
         
         
         
         
         
         
         
      •  
         
         
        • 示例1:
        • 需求:匹配出0-100之间的数字
          • import re
            ret = re.match("[1-9]?d","8")
            print(ret.group()) #8
            
            ret = re.match("[1-9]?d","78")
            print(ret.group()) #78
            
            #不正确的情况
            ret = re.match("[1-9]?d","08")
            print(ret.group())  #0
            
            #修正之后
            ret = re.match("[1-9]?d$","08")
            if ret:
                print(ret.group())
            else:
                print("不在0-100之间")
            
            #添加|
            ret = re.match("[1-9]?d$|100","8")
            print(ret.group())  #8
            
            ret = re.match("[1-9]?d$|100","78")
            print(ret.group())  #78
            
            ret = re.match("[1-9]?d$|100","08")
            #print(ret.group())  #不在0到100之间,故错
            
            ret = re.match("[1-9]?d$|100","100")
            print(ret.group()) #100
            
            #运行结果
            8
            78
            0
            不在0-100之间
            8
            78
            100
        • 示例2:()  
        • 需求:匹配出163、126、qq邮箱
          • import re
            ret = re.match("w{4,20}@16..com","test@163.com")
            print(ret.group())  #test@163.com
            
            ret = re.match("w{4,20}@(163|126|qq).com","test@126.com")
            print(ret.group())  #test@126.com
            
            ret = re.match("w{4,20}@(163|126|qq).com","test@qq.com")
            print(ret.group())  #test@qq.com
            
            ret = re.match("w{4,20}@(163|126|qq).com","test@sina.com")
            if ret:
                print(ret.group())
            else:
                print("不是163、126、qq邮箱")   #不是163、126、qq邮箱
            
            
            
            #运行结果
            test@163.com
            test@126.com
            test@qq.com
            不是163、126、qq邮箱



        • 示例3:
        • 需求:匹配出<html>hh</html>
          • import re
            
            #能够完成对正确的字符串的匹配
            ret = re.match("[<a-zA-Z>]*w</[a-zA-Z]*>","<html>hh</html>")
            print(ret.group())    #<html>hh</html>
            
            #如果遇到非正常的html格式字符串,匹配出错
            ret = re.match("[<a-zA-Z>]*w</[a-zA-Z]*>","<html>hh</htmlbalabal>")
            print(ret.group())  #<html>hh</htmlbalabal>
            
            #正确的理解思路 :如果在第一对<>中是什么,按理来说后面的那对<>中就应该是什么
            
            #通过引用分组中匹配到的数据即可,但是要注意是元字符串,即类似r""这种格式
            ret = re.match(r"<([a-zA-Z]*)>w*</1>", "<html>hh</html>")
            print(ret.group())  #<html>hh</html>
            
            
            #因为2对<>中的数据不一致,所以匹配不出来
            test_label = "<html>hh</htmlbalabala>"
            ret= re.match(r"<([a-zA-Z]*)>w*</1>",test_label)
            if ret:
                print(ret.group())
            else:
                print("%s 这是一个不对的标签" % test_label)    #<html>hh</htmlbalabala> 这是一个不对的标签
            
            
            #运行结果
            <html>hh</html>
            <html>hh</htmlbalabal>
            <html>hh</html>
            <html>hh</htmlbalabala> 这是一个不对的标签
        •  模式修正符
          •  re.S  让.也可以匹配多行
          •  re.I   让匹配时忽略大小写
  • 相关阅读:
    多图详解!10大高性能开发核心技术(转发)
    从 Spring Cloud 看一个微服务框架的「五脏六腑」
    eclipse中的springBoot项目 执行maven build 和maven install 报错
    Mysql怎么删除某表中的一条数据
    eclipse 中需要配置jdk、需要配置jre吗? 以及安装eclipse后需要做的一些配置
    IntelliJ IDEA 2019.2最新版本免费激活码(亲测可用)
    在springBoot项目配置项目的访问路径的时候 server.context-path不起作用的原因
    共享类型的基站概念
    oracle创建索引
    ORACLE中的DBLINK概念及使用DBLINK对远程数据库的连接
  • 原文地址:https://www.cnblogs.com/u-damowang1/p/12623665.html
Copyright © 2011-2022 走看看