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

    正则表达式是处理字符串的强大的工具,它有自己特定的语法结构,有了它,实现字符串的检索、替换、匹配验证都不在话下。

    .

    匹配任意字符,除了换行符,当re.DOTALL标记被指定时,则可以匹配包括换行符的任意字符

    import re
    a = 'xz123'
    b = re.findall('x....',a)
    print(b)
    运行结果:
    ['xz123']
    

    *

    匹配0个或多个的表达式

    import re
    a = 'xyxy123'
    b = re.findall('x*',a)
    print(b)
    运行结果:
    ['x', '', 'x', '', '', '', '', '']
    

    ?

    匹配0个或1个由前面的正则表达式定义的片段,非贪婪方式

    import re
    a = 'xy123'
    b = re.findall('x?',b)
    print(b)
    运行结果:
    ['x', '', '', '', '', '']
    

    .*的使用举例

    import re
    secrect_code = 'hadkfalifexxIxxfasdjifja134xxlovexx23345sdfxxyouxx8dfse'
    b = re.findall('xx.*xx',secrect_code)
    print(b)
    运行结果:
    ['xxIxxfasdjifja134xxlovexx23345sdfxxyouxx']
    

    .*?的使用举例

    secrect_code = 'hadkfalifexxIxxfasdjifja134xxlovexx23345sdfxxyouxx8dfse'
    b = re.findall('xx.*?xx',secrect_code)
    print(b)
    运行结果:
    ['xxIxx', 'xxlovexx', 'xxyouxx']
    

    (.*?)使用括号与不使用括号的差别

    ( )匹配括号内的表达式,也表示一个组

    import re
    secrect_code = 'hadkfalifexxIxxfasdjifja134xxlovexx23345sdfxxyouxx8dfse'
    d = re.findall('xx(.*?)xx',secrect_code)
    print(d)
    for each in d:
        print(each)
    运行结果:
    ['I', 'love', 'you']
    I
    love
    you
    

    re.S的作用使.的作用包括了

    import re
    s = '''sdfxxhelloxxfsdf
    xxworldxxasdf'''
    d = re.findall('xx(.*?)xx',s,re.S)
    print(d)
    运行结果:
    ['hello', 'world']
    

    $

    匹配字符串的末尾

    import re
    s= 'detail.html?key=TISSUE INJURY'
    f = re.findall('key=(.*?)',s)
    print(f)
    运行结果:
    ['']
    
    import re
    s= 'detail.html?key=TISSUE INJURY'
    f = re.findall('key=(.*?)$',s)
    print(f)
    运行结果:
    ['TISSUE INJURY']
    

    d

    匹配任意数字,等价于 [0-9]

    import re
    s = 'asdfasf1234567fasd555fas'
    b = re.findall('(d+)',s)
    print(b)
    运行结果:
    ['1234567', '555']
    
    import re
    s = 'asdfasf1234.567fasd55.5fas'
    b = re.findall('(d+.d+)',s)
    print(b)
    运行结果:
    ['1234.567', '55.5']
    
    

    转义字符

    import re
    s = 'sda123sdaslda.s/'
    b = re.search('.',s)
    print(b)
    print(b.span())
    print(b.group())
    运行结果:
    <_sre.SRE_Match object; span=(13, 14), match='.'>
    (13, 14)
    .
    

    a|b

    匹配a或b

    import re
    s = 'fishdafishc'
    b= re.search(r'fish(c|d)',s)
    print(b)
    运行结果:
    <_sre.SRE_Match object; span=(0, 5), match='fishd'>
    

    {n}

    精确匹配n个前面表达式**

    import re
    s ='i fishhhfish.a'
    b = re.search(r'fish{3}',s)
    print(b.group())
    运行结果:
    fishhh
    
    
    s  = 'i fishhhfishfishfish.a'
    b = re.search(r'(fish){3}',s )
    print(b.group())
    运行结果:
    fishfishfish
    

    {n, m}

    匹配 n 到 m 次由前面的正则表达式定义的片段,贪婪方式**

    import re
    s = 'i fishhhfishfishfish.a'
    b = re.search(r'(fish){2,5}',s)
    print(b.group())
    运行结果:
    fishfishfish
    

    sub的使用举例

    import re
    s = '123rrrrr123'
    output = re.sub('123(.*?)123','123%d123'%789,s)
    print(output)
    运行结果:
    123789123
    
    #把一串文本中的所有数字都去掉
    content = '54aK54yr5oiR54ix5L2g'
    content = re.sub('d+', '', content)
    print(content)
    运行结果:
    aKyroiRixLg
    

    findall,search和match的区别

    match

    从首字母开始开始匹配,string如果包含pattern子串,则匹配成功,返回Match对象

    若string中包含pattern子串,则返回Match对象,否则返回None,注意,如果string中存在多个pattern子串,只返回第一个

    findall

    返回string中所有与pattern相匹配的全部字串,返回形式为数组

    >>> import re
    >>> s = '123.gifgdfkgj.jpg1673.gifgdfk66.jpg'
    >>> m = re.match(r'(d*)(.gif|.jpg)',s)
    >>> m.group()
    '123.gif'
    >>> m.group(0)
    '123.gif'
    >>> m.group(1)
    '123'
    >>> m.group(2)
    '.gif'
    >>> m.groups()
    ('123', '.gif')
    ------------------------------------------------------------
    >>> m = re.findall(r'(d*)(.gif|.jpg)',s)
    >>> m
    [('123', '.gif'), ('', '.jpg'), ('1673', '.gif'), ('66', '.jpg')]
    ------------------------------------------------------------
    >>> s = 'gdfkg1.jpg1673.gifgdfk66.jpg'
    >>> m = re.search(r'.(d*)(.gif|.jpg)',s)
    >>> m
    <_sre.SRE_Match object; span=(4, 10), match='g1.jpg'>
    >>> m.group()
    'g1.jpg'
    >>> m.group(0)
    'g1.jpg'
    >>> m.group(1)
    '1'
    >>> m.group(2)
    '.jpg'
    >>> m.group(3)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    IndexError: no such group
    >>> m.groups()
    ('1', '.jpg')
    

    re.escape(string)

    对字符串中的非字母数字进行转义
    import re
    delimiter =  ',.'
    a = re.escape(delimiter)
    print(a)
    print(re.findall(re.escape('w.py'),"jw.pyji w.py.f"))
    
    \,.
    ['w.py', 'w.py']

    模式描述

    次数/个数

    *匹配0个或多个的表达式。
    +匹配1个或多个的表达式。
    ?匹配0个或1个由前面的正则表达式定义的片段,非贪婪方式
    {n}精确匹配n个前面表达式
    {n,} 至少有n次
    {n, m}匹配 n 到 m 次由前面的正则表达式定义的片段,贪婪方式

    范围

    [amk]-----字符集合
     匹配 'a''m''k'。三者中的任何一个
    [^abc]------负值字符集合
    匹配除了a,b,c之外的字符
    [0-9]------数字范围
    [a-z]------字符范围
    [^a-z]------负值字符范围
    w匹配字母数字及下划线
    W匹配非字母数字及下划线
    s匹配任意空白字符,等价于 [	
    
    f].
    S匹配任意非空字符
    d匹配任意数字,等价于 [0-9]
    D匹配任意非数字
    A匹配字符串开始
    匹配字符串结束,如果是存在换行,只匹配到换行前的结束字符串
    z匹配字符串结束
    G匹配最后匹配完成的位置
    
    匹配一个换行符
    	匹配一个制表符
    ^匹配字符串的开头
    $匹配字符串的末尾。
    .匹配任意字符,除了换行符,当re.DOTALL标记被指定时,则可以匹配包括换行符的任意字符。
    a|b匹配a或b
    ( )匹配括号内的表达式,也表示一个组
  • 相关阅读:
    git version info & svn version info map(七)
    /proc/pid/statm content analysis
    git log filter(六)
    git create remote branch (五)
    learning svn diff --summarize
    learning scala akka ask_pattern
    learning scala akka tell pattern(二)
    learning scala akka actorySystem create and close
    hibernate 自动生成数据库
    STRICT_TRANS_TABLES STRICT_ALL_TABLES
  • 原文地址:https://www.cnblogs.com/feifeifeisir/p/13689783.html
Copyright © 2011-2022 走看看