zoukankan      html  css  js  c++  java
  • Python re 模块

    python 使用 re 模块来使用正则表达式对字符串进行匹配,常用的方法如下:

    re.findall(pattern, string)搜索 string,以列表的形式返回全部能匹配的子串

    In [2]: re.findall(r'o', 'hello world')
    Out[2]: ['o', 'o']
    
    In [3]: re.findall(r'ab+', 'absfsdjabbcd')
    Out[3]: ['ab', 'abb']

    re.S表示可以用点(.)来匹配换行符,如下,不加 re.S 是不能匹配 的

    In [4]: re.findall(r'.', 'a
    b
    c')
    Out[4]: ['a', 'b', 'c']
    
    In [5]: re.findall(r'.', 'a
    b
    c', re.S)
    Out[5]: ['a', '
    ', 'b', '
    ', 'c']

    re.match(pattern, string)搜索 string,只从开头的位置进行匹配,匹配成功返回一个对象,需要使用对象的 group() 方法来获取匹配到的内容;如果匹配不成功返回None

    In [14]: m = re.match(r'he', 'hello world')
    
    In [15]: m.group()
    Out[15]: 'he'
    In [16]: m = re.match(r'(w+)s(w+)', 'hello world')    # 也可以采用分组的形式来匹配
                                                             # group(1)可以查看匹配到的第一个分组
    In [24]: m.group(1)                                      # group(2)可以查看匹配到的第二个分组
    Out[24]: 'hello'                                         # groups()可以查看匹配到的所有分组
    
    In [25]: m.group(2)
    Out[25]: 'world'
    
    In [26]: m.groups() 
    Out[26]: ('hello', 'world')

    re.search(pattern, string)搜索 string,只匹配第一个符合规则的字符串,匹配成功则返回一个对象,需要使用对象的 group() 方法来获取匹配到的内容;未匹配成功返回None

    In [42]: m = re.search(r'l', 'hello world')
    
    In [46]: m.group() 
    Out[46]: 'l'

    re.sub(pattern, repl, string, count=0)用于替换字符串,count=0 可以指定替换的次数,默认全部替换

    In [48]: re.sub(r'l', 'L', 'hello world')
    Out[48]: 'heLLo worLd'
    
    In [49]: re.sub(r'l', 'L', 'hello world', count=2)
    Out[49]: 'heLLo world'

    re.compile(pattern)用于将正则表达式编译成一个对象,然后再用这个对象去调用 findall() 等方法进行匹配,这样做可以提高匹配的效率

    In [53]: pattern = re.compile(r'l+')
    
    In [54]: pattern.findall('hello world')
    Out[54]: ['ll', 'l']

    re.I用于指定在进行正则匹配时忽略大小写,如下,如果不加上 re.I 只会匹配小写字符

    In [55]: re.findall(r'l', 'hello worLd')
    Out[55]: ['l', 'l']
    
    In [56]: re.findall(r'l', 'hello worLd', re.I)
    Out[56]: ['l', 'l', 'L']

    re.M用于指定多行匹配,只影响 ^ 和 $,如下,如果我们不加 re.M ,默认只匹配第一行,加了 re.M 会匹配多行,通常用在对文件的匹配上

    In [63]: re.findall(r'^a', 'aaa
    abc
    ack') 
    Out[63]: ['a']
    
    In [64]: re.findall(r'^a', 'aaa
    abc
    ack', re.M)
    Out[64]: ['a', 'a', 'a']
    In [65]: with open('/etc/passwd') as fd:
       ....:     data = fd.read()
       ....:     
    
    In [66]: re.findall(r'^ftp', data)          # 如果不加re.M,则表示只对第一行进行正则匹配
    Out[66]: [] 
    
    In [67]: re.findall(r'^ftp', data, re.M)    # 如果加上re.M,则表示对每一行进行正则匹配
    Out[67]: ['ftp']

    re.X如果使用该方法,那么正则表达式可以使用多行来写,如下,如果不使用 re.X,那么我们只能把正则表达式写在一行,加上 re.X 可以写成多行

    In [68]: mail = '1210640219@qq.com'
    
    In [69]: re.findall(r'd{1,10}@w+.[a-z]{3}', mail)  
    Out[69]: ['1210640219@qq.com']
    
    In [72]: re.findall(r'''
       ....: d{1,10}
       ....: @
       ....: w+
       ....: .
       ....: [a-z]{3}
       ....: ''', mail, re.X)
    Out[72]: ['1210640219@qq.com']

         

        

  • 相关阅读:
    [LeetCode-JAVA] Count Complete Tree Nodes
    [LeetCode-JAVA] Shortest Palindrome
    [LeetCode-JAVA] Best Time to Buy and Sell Stock IV
    [LeetCode-JAVA] Word Ladder II
    [LeetCode-JAVA] Jump Game II
    Keil开发的ARM程序main函数之前的汇编分析
    STM32平台SD卡的FatFS文件系统开发
    STM32 Cortex-M3 NMI异常
    应对STM32 Cortex-M3 Hard Fault异常
    LwIP协议栈开发嵌入式网络的三种方法分析
  • 原文地址:https://www.cnblogs.com/pzk7788/p/10320705.html
Copyright © 2011-2022 走看看