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

    Python正则表达式指南

    import re
    
    #
    # . 代表任何单一字符
    # * 代码任意一个它之前的字符
    #  .*代表任意多个字符(包括0个)
    
    #match
    #match只能检测以模式串为开头的源字符串
    source = 'Young Frankenstein'
    pattern = re.compile('You')
    result = pattern.match(source)
    if result:
        print(result.group()) #You
    
    print(re.match('Frank', source)) #None
    print(re.match('.*Frank', source)) #<_sre.SRE_Match object; span=(0, 11), match='Young Frank'>
    
    #search
    print(re.search('Frank', source)) #<_sre.SRE_Match object; span=(6, 11), match='Frank'>
    
    #findall
    result = re.findall('n', source)
    print(result) #['n', 'n', 'n', 'n']
    result = re.findall('n.', source)
    print(result) #['ng', 'nk', 'ns']
    
    #split
    print(re.split('n', source)) #['You', 'g Fra', 'ke', 'stei', '']
    
    #sub 替换
    print(re.sub('n', '?', source)) #You?g Fra?ke?stei?

    特殊的字符

    使用标识符

  • 相关阅读:
    day_5.25py
    day_5.24py
    day_5.22 py
    numpy模块 03
    requests 模块例题示范
    logging模块
    typing 模块
    hashlib模块和hmac模块
    random模块
    time和datetime模块
  • 原文地址:https://www.cnblogs.com/jzm17173/p/5436144.html
Copyright © 2011-2022 走看看