• 【python3】5.正则表达式


    本学习内容总结于莫烦python:5.正则表达式
    https://mofanpy.com/tutorials/python-basic/interactive-python/regex
    
    • 1
    • 2

    5.正则表达式

    本章较为重要,单独拿出来成章了

    正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配 比如我要批量修改很多文件中出现某种固定模式的文字时。
    re 模块使 Python 语言拥有全部的正则表达式功能

    5.1 不用正则判断

    pattern1 = "file"
    pattern2 = "files"
    string = "the file is in the folder"
    print("file in string", pattern1 in string)   
    print("files in string", pattern2 in string)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    file in string True
    files in string False
    
    • 1
    • 2

    做了一个判断,看有没有包含这个词。不过种类变多了之后,总写这样的判断,处理的能力是十分有限的。

    正则的方法

    方法一:规则用命名=re.compile(r);用命名.search("")比较

    import re
    # 命名=re.compile(r)
    ptn = re.compile(r"\w+?@\w+?\.com")
    
    #`命名.search()
    matched = ptn.search("mofan@mofanpy.com")
    print("mofan@mofanpy.com is a valid email:", matched)
    matched = ptn.search("mofan@mofanpy+com")
    print("mofan@mofanpy+com is a valid email:", matched)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    \w:匹配所有字母数字,等同于 [a-zA-Z0-9_]
    +:+号之前的字符出现 >=1 次
    ?: 在符号前面的字符为可选,即出现 0 或 1 次
    +?:则是惰性匹配,不加是尽可能多的配,先尽可能少的配,这里: \w 至少匹配 1 次

    现在有个字符串adsabopbc

    • 正则是a.+b,这种情况下这个正则表达式能匹配的就是adsabopb,因为中间的.+会保证在尽量能匹配成功的前提下尽量的多匹配字符;
    • 正则是a.+?b,那么匹配的结果就是adsab了,因为他会保证在尽量能匹配的成功的情况下少的匹配字符。

    我都是用 r"xxx" 来写一个 pattern:因为正则表达式很多时候都要包含\,r 代表原生字符串, 使用 r 开头的字符串是为了不让你混淆 pattern 字符串中到底要写几个 \,你只要当成一个规则来记住在写 pattern 的时候,都写上一个 r 在前面就好了。

    mofan@mofanpy.com is a valid email: <re.Match object; span=(0, 17), match='mofan@mofanpy.com'>
    mofan@mofanpy+com is a valid email: None
    
    • 1
    • 2

    re.Match object这个意思应该是匹配了 ,匹配的范围是[0,17),匹配内容是:mofan@mofanpy.com
    没匹配 返回没有None

    • 方法二:re.search(r"规则","对象")
    matched = re.search(r"\w+?@\w+?\.com", "the email is mofan@mofanpy.com.")
    print("the email is mofan@mofanpy.com:", matched)
    
    • 1
    • 2
    the email is mofan@mofanpy.com: <re.Match object; span=(13, 30), match='mofan@mofanpy.com'>
    
    • 1

    5.2 正则给额外信息

    • 提出匹配的内容match.group()
    match = re.search(r"\w+?@\w+?\.com", "the email is mofan@mofanpy.com.")
    print(match)
    print(match.group())
    
    • 1
    • 2
    • 3
    <re.Match object; span=(13, 30), match='mofan@mofanpy.com'>
    mofan@mofanpy.com
    
    • 1
    • 2

    此处内容看视频】:https://www.bilibili.com/video/BV1ef4y1U7V4/

    • |:或 字符串
    • [au]:相当于字母间的a|u

    在这里插入图片描述

    re.search(<
    • 相关阅读:
      鸿蒙原生应用元服务-访问控制(权限)开发应用权限列表二
      【强化学习】gymnasium自定义环境并封装学习笔记
      APM32F0XX/STM32F0XX停机模式功耗测试
      Tomcat 8.0下载与配置
      WPF学习笔记-FlowDocument流文档基础知识和基本操作
      使用 pnpm monorepo + ts 制作个功能完善的 CLI 命令行工具
      在vmware中给linux添加硬盘
      信息检索与数据挖掘 | (五)文档评分、词项权重计算及向量空间模型
      js 计算两个日期间所有的日期(moment.js实现)
      Ubuntu-Ports更新源 ARM64更新源
    • 原文地址:https://blog.csdn.net/wistonty11/article/details/127891677