zoukankan      html  css  js  c++  java
  • (学习)python非贪婪、多行匹配正则表达式例子 分类: 正则表达式 2013-08-19 11:21 2540人阅读 评论(2) 收藏

    一些regular的tips:

    1 非贪婪flag

    >>> re.findall(r"a(d+?)","a23b") # 非贪婪模式
            
    ['2']
    >>>
    re.findall(r"a(d+)","a23b")
            
    ['23']

    注意比较这种情况:

    >>> re.findall(r"a(d+)b","a23b")
            
    ['23']
    >>>
    re.findall(r"a(d+?)b","a23b") #如果前后均有限定条件,则非匹配模式失效
            
    ['23']

    2 如果你要多行匹配,那么加上re.S和re.M标志
    re.S:.将会匹配换行符,默认.逗号不会匹配换行符

    >>> re.findall(r"a(d+)b.+a(d+)b","a23b a34b")
            
    []
    >>>
    re.findall(r"a(d+)b.+a(d+)b","a23b a34b",re.S)
            
    [('23','34')]
    >>>

    re.M:^$标志将会匹配每一行,默认^只会匹配符合正则的第一行;默认$只会匹配符合正则的末行

    >>> re.findall(r"^a(d+)b","a23b a34b")
            
    ['23']
    >>>
    re.findall(r"^a(d+)b","a23b a34b",re.M)
            
    ['23','34']

    但是,如果没有^标志,

    >>> re.findall(r"a(d+)b","a23b a34b")
            
    ['23','43']

    可见,是无需re.M


    import re

    n='''12 drummers drumming,
    11 pipers piping, 10 lords a-leaping'''

    p=re.compile('^d+')
    p_multi=re.compile('^d+',re.MULTILINE) #设置 MULTILINE 标志
    print re.findall(p,n)  #['12']
    print re.findall(p_multi,n) # ['12', '11']

    ============================

    import re
    a = 'a23b'
    print re.findall('a(d+?)',a)  #['2']
    print re.findall('a(d+)',a) #['23']
    print re.findall(r'a(d+)b',a) #['23']
    print re.findall(r'a(d+?)b',a) # ['23']
    ============================
    b='a23b a34b'
    ''' . 匹配非换行符的任意一个字符'''

    print re.findall(r'a(d+)b.+a(d+)b',b) #[]

    print re.findall(r'a(d+)b',b,re.M) # ['23', '34']

    print re.findall(r'^a(d+)b',b,re.M) # ['23', '34']

    print re.findall(r'a(d+)b',b) #['23','34'] 可以匹配多行

    print re.findall(r'^a(d+)b',b) # ['23'] 默认^只会匹配符合正则的第一行

    print re.findall(r'a(d+)b$',b) # ['34'] 默认$只会匹配符合正则的末行

    print re.findall(r'a(d+)b',b,re.M) #['23', '34']

    print re.findall(r'a(d+)b.?',b,re.M)  # ['23', '34']

    print re.findall(r"a(d+)b", "a23b a34b")  # ['23', '34']

    ============================

  • 相关阅读:
    恭介的法则
    229. Majority Element II
    169. Majority Element
    233. Number of Digit One
    172. Factorial Trailing Zeroes
    852. Peak Index in a Mountain Array
    162. Find Peak Element
    34. Find First and Last Position of Element in Sorted Array
    81. Search in Rotated Sorted Array II
    33. Search in Rotated Sorted Array
  • 原文地址:https://www.cnblogs.com/think1988/p/4628087.html
Copyright © 2011-2022 走看看