• 正则表达式re模块的使用教程『更新中』


    import os
    import re
    from pathlib import Path, PurePath
    
    • 1
    • 2
    • 3

    使用match函数只能检测匹配的第一个字符

    pattern = "s"
    strs = "sdsjflakdhfpsa"
    
    res = re.match(pattern, strs)
    print(res) # 如果第一个字符匹配成功了则返回类型信息和字符信息
    print(res.group()) # 使用group函数,才可以另返回值是匹配的字符
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    
    s
    
    • 1
    • 2

    使用findall实现所有字符的匹配

    res = re.findall(pattern, strs)
    print(res)
    
    • 1
    • 2
    ['s', 's', 's']
    
    • 1

    元字符

    #. 表示非\n的任意字符
    print(re.match(".", "123456789"))
    # \d 匹配任意的数字0~9
    print(re.match("\d", "123456789"))
    # \D 匹配非数字0~9(任意大写都表示非)
    print(re.match("\D", "a_123456789")) 
    # 每一个方括号表示只匹配一个字符
    # \s表示空白字符,就是看不到的字符, 如\n\t, "\S"与之相反
    print(re.match("\s\s", "\n\t"))
    # \w 大小写字母,数字和下划线 “\W”与之相反
    print(re.match("\w\w\w\w", "_Aa123456789"))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    
    
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    可以使用范围, 一个中括号表示一个字符位置

    print(re.match("[0-9][0-9]", "123456789"))
    print(re.match("[a-e]", "asddfffgg"))
    print(re.match("[w-z]", "xsfasdff"))
    print(re.match("[0-9a-e]", "123456789"))# 第一个位置不管是0-9内还是a-e内都可以匹配
    
    • 1
    • 2
    • 3
    • 4
    
    
    
    
    
    • 1
    • 2
    • 3
    • 4

    多字符的匹配

    print(re.match("\d\d\d\d\d\d\d\d\d", "123456789"))
    # 等价于
    print(re.match("\d*", "123456789")) # “*”使用任意次的“\d”
    print(re.match("\d*", "12345a6789")) # 一直向后匹配直到遇到非数字
    print(re.match("\d+", "12a3456789")) # “+”之前一定要出现至少一次数字才能匹配
    print(re.match("\d+", "a12a3456789")) # “a”之前一次都没有出现,所以是None
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    
    
    
    
    None
    
    • 1
    • 2
    • 3
    • 4
    • 5

    次数匹配

    print(re.match("\d{3}", "1234a3456789")) # 之前一定要至少出现3次,才可以拿到3个位置的字符
    print(re.match("\d{3,}", "1234a3456789")) # 之前一定要至少出现3次,才可以拿到所有的字符
    print(re.match("\d{3,6}", "1234345a6789")) # 之前一定要至少出现3到6次之间,才可以拿到其中的所有的字符
    
    • 1
    • 2
    • 3
    
    
    
    
    • 1
    • 2
    • 3

    边界处理

    # 匹配一个电话号码
    tel = "13345678910aa298097"
    print(re.match("^1[358][1-9]\d{8}$", tel)) # "^"表示开头,“$”表是结尾
    # "\b"表示边界, \B与之相反 
    str = "Welcome to Longman Dictionary of Contemporary English Online"
    print(re.findall('an\\b', str)) # 以an结尾的内容
    print(re.findall('\\bLon', str)) # 以Lon开头的内容
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    None
    ['an']
    ['Lon']
    
    • 1
    • 2
    • 3

    分组匹配

    t = "2023-10-30fagd"
    print(re.match("\d{4}-(0[1-9]|1[0-2])-([0-2][0-9]|3[0-1])", t))# "|" 这个符号为或者,使用是必须在两边加小括号表示作用域, 小括号为分组使用 
    print(re.match("\d{4}-(0[1-9]|1[0-2])-([0-2][0-9]|3[0-1])", t).group())
    print(re.match("\d{4}-(0[1-9]|1[0-2])-([0-2][0-9]|3[0-1])", t).group(0))
    print(re.match("\d{4}-(0[1-9]|1[0-2])-([0-2][0-9]|3[0-1])", t).group(1))
    print(re.match("\d{4}-(0[1-9]|1[0-2])-([0-2][0-9]|3[0-1])", t).group(2))
    print(re.match("(\d{4})-(0[1-9]|1[0-2])-([0-2][0-9]|3[0-1])", t).group(1))# "0"为全部,“1表示第一组”,“2表示第二组”, “2表示第三组”
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    
    2023-10-30
    2023-10-30
    10
    30
    2023
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    正则表达式中的元字符,量词:贪婪和非贪婪,转义符: \s: 记得使用-z --null-data: 使用ascii码中空字符来替换新行,分组:““,和‘‘
    qps、tps、吞吐量
    事务-Java Spring
    【LeetCode】148. 排序链表
    (十)ElasticSearch高级使用【别名,重建索引,refresh操作,高亮查询,查询建议】
    c++之泛型算法
    领域驱动设计——模型
    小猫来了~Tomcat以及安装
    [ vulhub漏洞复现篇 ] 阿里巴巴Nacos身份验证绕过(nacos未授权)(CVE-2021-29441)
    Linux mmap原理
  • 原文地址:https://blog.csdn.net/m0_46114594/article/details/133965494