zoukankan      html  css  js  c++  java
  • python正则表达式

    正则表达式首先调用re模块

    import re

    1、match方法

      match(pattern, string, flags=0)  #re.match的函数签名

    match要求从开头匹配
    a = re.match('test',  'test123')
    print(a)
    
    b = re.match('test', 'ltest1234')
    print(b)
    #b的结果为None,说明没有匹配到

    2、search方法

    search(pattern, string, flags=0)

    search不需要从第一个字符开始匹配

    a = re.search('test', 'test1234')
    print(a)
    
    b = re.search('test', 'lleatest4323')
    print(b)

    无论a和b都是可以匹配到的

    3、元字符metacharacters

      .    表示匹配除换行符外的任意字符

      w 表示匹配字母或数字或下划线或汉字

          s   表示匹配任意空白字符,包括空格、制表符、换页付等

      d 表示数字

        匹配单词开始或结束

    print(re.search('.....','hello'))
    print(re.search('wwww', 'a1_啥'))
    print(re.search('ss', '	
    '))
    print(re.search('dd', '12'))

      ^匹配行首

          $匹配行尾

    print(re.search('^h.*w$','hello'))

      x|y 匹配x或者y

      [xyz] 匹配任意一个字符

      [a-z]匹配字符范围,也是匹配任意一个字符

    print(re.search('a|e', 'abble'))
    print(re.search('[a12]','abcd'))

    4、重复

      ?匹配前面的字表达式零次或一次

      +匹配前面字表达式一次或者多次

      *匹配前面子表达式零次或者多次

      {n}重复n次

      {n,}最少重复n次

      {,m}最多重复m次

    print(re.search('d{5}', '12345'))
    print(re.search('ca*t', 'cart'))
    print(re.search('ca*t', 'cat'))
    print(re.search('ca*t', 'caat'))

    5、反义

    [^x] 匹配除了x以外的任意字符

    [^abc] 匹配除了abc这几个字母以外的任意字符

    W  匹配任意不是字母、数字、下划线、汉字的字符等价于[^A-Za-z0-9_]

    S 匹配任意不是空白的字符 等价于[^f v]

    D 匹配任意非数字的字符 [^0-9]

    B 匹配不是单词开头或者结束的位置

    6、贪婪与懒惰

    默认情况下正则表达式是贪婪模式

    print(re.search('a.*b', 'aabab').group())
    #aabab

     *? 重复任意次,但尽可能少重复

    +?重复一次或多次,但是尽可能少重复

    ??重复0次或1次,但尽可能少重复

    {n,m}? 尽可能少重复 n到m次

    print(re.search('a.*?b', 'abadbadb').group())
    #ab

    7、编译标志

    DOTALL, S  使 .匹配包括换行在内的所有字符

    IGNORECASE, I  使匹配对大小写不敏感

    LOCALE  L  使本地化识别匹配

    MULTILINE, M 多行匹配影响^    $

    VERBOSE,  X  详细状态

    DEBUG  调试模式

    print(re.search('.', '
    '))
    #None
    
    print(re.search('.', '
    ', re.S))
    
    print(re.search('a.', 'A
    ', re.S|re.I))

    8、编译正则表达式

    regex = re.compile(r'^d{1,3}$')
    
    print(regex.match('12'))
    print(regex.match('1234'))

    9、检索替换

    re.sub(pattern, repl, string, count=0, flags=0)

    print(re.sub('d+', '', 'test123'))
    print(re.sub('d', '', 'test123test13451tesa'))
    #输出一样,说明替换不是只替换一次,而是贪婪模式,全部替换
    
    print(re.sub('d', '', 'test123', count=2))

    10、findall/finditer

      findall会一次返回所有匹配的数值并放入列表,finditer会返回一个迭代器

    print(re.findall('d', '1a2b3c4d5e6'))
    
    for i in re.finditer('d', '1a2d3e4r5ft6rq'):
        print(i)

    11、分组

    m = re.compile(r'(a)b')
    a = m.match('ab')
    print(a.group(1))
    
    m = re.compile(r'([a-c]+).*(w)')
    a = m.match('abcbde')
    print(a.group(1), a.group(2), a.group(1, 2))

    12、命名分组

    (?P<name>正则表达式) #命名分组格式

    pattern = '(?P<year>d{4})-(?P<month>d{2})-(?P<day>d{2})'
    
    m = re.match(pattern, '2018-01-02')
    print(m.groupdict())
  • 相关阅读:
    6 完全平方数相关
    5 三位数,每个位置不同
    Neo4j Admin Import 导入多个node和relationship
    Rust所有权
    Rust 多态
    Rust 泛型
    Rust trait
    Rust模块化
    Spring Cloud(Dalston.SR1)
    git 速度慢问题解决
  • 原文地址:https://www.cnblogs.com/xiaopi-python/p/8653788.html
Copyright © 2011-2022 走看看