美文网首页
re模块基本使用方法

re模块基本使用方法

作者: 寒火儿 | 来源:发表于2018-08-02 18:37 被阅读175次

    re.match()

    re.match 尝试从字符串的起始位置匹配一个模式,如果匹配失败,match()返回none

    re.match(pattern, string, flags=0)

    最常规的匹配

    import re
    
    content = 'Hello 11223 44445 World_This is Regex Demo'
    result = re.match('^Hello\s\d\d\d\d\d\s\d{5}\s\w{10}.*Demo$', content)
    print(result)
    print(result.group())
    print(result.span())
    
    <_sre.SRE_Match object; span=(0, 42), match='Hello 11223 44445 World_This is Regex Demo'>
    Hello 11223 44445 World_This is Regex Demo
    (0, 42)
    

    泛匹配

    import re
    
    content = 'Hello 11223 44445 World_This is Regex Demo'
    result = re.match('^Hello.*Demo$', content)
    print(result)
    print(result.group())
    print(result.span())
    
    <_sre.SRE_Match object; span=(0, 42), match='Hello 11223 44445 World_This is Regex Demo'>
    Hello 11223 44445 World_This is Regex Demo
    (0, 42)
    

    匹配目标

    import re
    
    content = 'Hello 11223 44445 World_This is Regex Demo'
    result = re.match('^Hello\s(\d+).*Demo$', content)
    print(result)
    print(result.group(1))
    print(result.span())
    
    <_sre.SRE_Match object; span=(0, 42), match='Hello 11223 44445 World_This is Regex Demo'>
    11223
    (0, 42)
    

    贪婪匹配

    .* 会匹配尽可能多的字符

    import re
    
    content = 'Hello 11223 44445 World_This is Regex Demo'
    result = re.match('^Hello.*(\d+).*Demo$', content)
    print(result)
    print(result.group(1))
    print(result.span())
    
    <_sre.SRE_Match object; span=(0, 42), match='Hello 11223 44445 World_This is Regex Demo'>
    5
    (0, 42)
    

    非贪婪匹配

    .*? 匹配尽可能少的字符
    这里,它匹配到第一个数字就停止了
    这样就达到我们想取出第一串数字的目的了

    import re
    
    content = 'Hello 11223 44445 World_This is Regex Demo'
    result = re.match('^Hello.*?(\d+).*Demo$', content)
    print(result)
    print(result.group(1))
    print(result.span())
    
    <_sre.SRE_Match object; span=(0, 42), match='Hello 11223 44445 World_This is Regex Demo'>
    11223
    (0, 42)
    

    匹配模式

    import re
    
    content = '''Hello 11223 44445 World_This 
    is Regex Demo
    '''
    result = re.match('^Hel.*?(\d+).*?Demo$', content, re.S)
    print(result)
    print(result.group(1))
    
    <_sre.SRE_Match object; span=(0, 43), match='Hello 11223 44445 World_This \nis Regex Demo'>
    11223
    

    我们添加re.S是为了给.的匹配模式扩展到整个字符串,包括\n换行符

    转义

    import re
    
    content = 'price is $5.00'
    result = re.match('price is \$5\.00', content)
    print(result)
    
    <_sre.SRE_Match object; span=(0, 14), match='price is $5.00'>
    

    可以使用反斜杠进行转义

    小结:尽量使用泛匹配,使用括号得到匹配目标,尽量使用非贪婪模式,有换行符要用re.S


    re.search()

    re.search 扫描整个字符串并返回第一个成功匹配的字符

    import re
    
    content = 'hei, stt Hello 11223 World_This is Regex Demo'
    result = re.search('Hello.*?(\d+).*Demo', content)
    print(result)
    print(result.group(1))
    print(result.span())
    
    <_sre.SRE_Match object; span=(9, 51), match='Hello 11223 44445 World_This is Regex Demo'>
    11223
    (9, 51)
    

    小结:因为match会限制字符串头部,所以能用search就不用match


    re.findall()

    re.findall 扫描整个字符串并返回所有匹配的字符

    import re
    
    content = '''
    <div class="info">
      <div class="more-meta">
        <h4 class="title">
          邻人之妻
        </h4>
        <p>
          <span class="author">
            [美] 盖伊·特立斯
          </span>
          <span class="publisher">
            上海人民出版社
          </span>
        </p>
      </div>
    </div>
    <div class="info">
      <div class="more-meta">
        <h4 class="title">
          深蓝的故事
        </h4>
        <p>
          <span class="author">
            深蓝
          </span>
          <span class="publisher">
            新星出版社
          </span>
        </p>
      </div>
    </div>
    '''
    
    results = re.findall('"title">(.*?)</h4>.*?"author">(.*?)</span>.*?"publisher">(.*?)</span>', content, re.S)
    for result in results:
        title, author, publisher = result
        print('title:', title.strip())
        print('author:', author.strip())
        print('publisher:', publisher.strip())
    
    title: 邻人之妻
    author: [美] 盖伊·特立斯
    publisher: 上海人民出版社
    title: 深蓝的故事
    author: 深蓝
    publisher: 新星出版社
    

    re.sub()

    re.sub 替换字符串中每一个匹配的子串后返回替换后的整个字符串

    import re
    
    content = 'hei, stt Hello 12345 World_This is Regex Demo'
    content = re.sub('\d+', 'str', content)
    print(content)
    
    hei, stt Hello str World_This is Regex Demo
    

    如果我们要替换的字符串包含它本身,我们可以给它用小括号分组,然后用反斜杠数字来引用

    import re
    
    content = 'hei, stt Hello 12345 World_This is Regex Demo'
    content = re.sub('(\d+)', r'\1 678910', content)
    print(content)
    
    hei, stt Hello 12345 678910 World_This is Regex Demo
    

    re.compile()

    re.compile 将正则表达式编译成正则对象
    只需要一次编译,就可以达到反复使用

    import re
    
    content = '''hei, stt Hello 12345 World_This 
    is Regex Demo'''
    pattern = re.compile('h.*?Demo', re.S)
    result = pattern.match(content)
    print(result)
    
    <_sre.SRE_Match object; span=(0, 46), match='hei, stt Hello 12345 World_This \nis Regex Demo'>
    

    相关文章

      网友评论

          本文标题:re模块基本使用方法

          本文链接:https://www.haomeiwen.com/subject/pfvsvftx.html