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

    为何产生:

    1、处理文本成为计算机常见的工作之一;

    2、对文本内容的搜索提取是一项比较复杂困难的工作

    3、为了快速方便处理上述问题,正则表达式技术应运而生,并逐渐发展为一个被大众语言使用的独立技术

    定义:

    即高级文本匹配模式,提供了搜索,替代等功能。其本质是由一系列特殊符号和字符组成的字串,这个字串即是正则表达式。这个表达式描述了字符和字符的重复行为,可以匹配某类特征的字符串。

    特点:

    1、方便进行检索和修改;

    2、支持语言众多

    3、灵活多样

    4、mongo正则类型,Django等框架作为URL匹配

    使用:

    python中有re模块,专门用来处理正则表达式

    re.findall(pattern,string)

    功能:使用正则表达式匹配字符串

    参数:pattern 正则表达式

       string 目标字符串

    返回值:一个列表(匹配到的所有内容)

    元字符:

    即正则表达式中有特殊含义的字符

    1、普通字符

    元字符:ab

    匹配规则:匹配相应的普通字符

    >>> re.findall('ab','ab37748aabb3004jadab')
    ['ab', 'ab', 'ab']

    2、或

    元字符:ab|cd

    匹配规则:匹配‘’|"两边任意字符

    >>> re.findall('ab|77','ab37748aabb3004jadab77')
    ['ab', '77', 'ab', 'ab', '77']

    *“|”两侧不要有没有用的空格,因为空格也会参与匹配

    >>> re.findall('ab | 77','ab37748aabb3004jadab77')
    []

    3、匹配单一字符:

    元字符:'.'

    匹配规则:匹配除了换行之外的任意字符

    >>> re.findall('a.b','a b37748a3bb3004jadab77')
    ['a b', 'a3b']

    4、匹配开始位置

    元字符:^

    匹配规则:匹配一个字符串的开始位置

    >>> re.findall('^ab','ab37748a3bb3004jadab77')
    ['ab']

    5、匹配结束位置

    元字符:$

    匹配规则:匹配目标字符串的结束位置

    >>> re.findall('ab$','ab37748a3bb3 ab 004jad ab')
    ['ab']

     6、匹配重复

    元字符:*

    匹配规则:匹配前面的正则表达式重复0次或者多次

    >>> re.findall('ab*','ab37748abb3 a abbb 004jad abb')
    ['ab', 'abb', 'a', 'abbb', 'a', 'abb']
    >>>注意a 的出现,是因为ab*可以匹配b为0,也就是匹配a

    7、匹配重复

    元字符:+

    匹配规则:匹配前面的正则表达式,重复一次或者多次

    ab+           -----------ab abb abbb

    >>> re.findall('.*','ab37748abb3 a abbb 004jad abb')
    ['ab37748abb3 a abbb 004jad abb', '']
    >>> re.findall('.+','ab37748abb3 a abbb 004jad abb')
    ['ab37748abb3 a abbb 004jad abb']
    >>>

    8、匹配重复:

    元字符:?

    匹配规则:匹配前面的正则表达式,重复0次或1次

    >>> re.findall('ab?','ab37748abb3 a abbb 004jad abb')
    ['ab', 'ab', 'a', 'ab', 'a', 'ab']
    >>>

    9、匹配重复

    元字符:{n}

    匹配规则:匹配前面的正则表达式,指定的重复次数

    >>> re.findall('ab{3}','ab37748abb3 a abbb 004jad abb')
    ['abbb']
    >>> re.findall('ab{2}','ab37748abb3 a abbb 004jad abb')
    ['abb', 'abb', 'abb']
    >>> re.findall('ab{1}','ab37748abb3 a abbb 004jad abb')
    ['ab', 'ab', 'ab', 'ab']
    >>>

     10、匹配重复

    元字符:{m,n}

    匹配规则:匹配前面的正则表达式重复m次到n次

    >>> re.findall('ab{2,3}','ab37748abb3 a abbb 004jad abb')
    ['abb', 'abbb', 'abb']
    >>>

    11、匹配字符集合

    元字符:[]

    匹配规则:匹配括号范围内的任意一个字符

    [a-z]

    [A-Z]

    [0-9]

    [123A-Za-z]

    >>> re.findall('[0-5a-zA-Z]','abZA96FM12z')
    ['a', 'b', 'Z', 'A', 'F', 'M', '1', '2', 'z']
    >>>

    12、匹配字符集合

    元字符:{^...]

    匹配规则:匹配除指定字符集外的任意字符

    >>> re.findall('[^0-5a-zA-X]','abZA96FM12z')
    ['Z', '9', '6']
    >>>

    >>> re.findall('[^0-5a-dA-X]','abZA96FM12z')
    ['Z', '9', '6', 'z']
    >>>

    13、匹配任意(非)数字字符

    元字符:d  D

    匹配规则:d匹配任意数字字符   等价于[0-9]

         D 匹配任意非数字字符  等价于[^]

    >>> re.findall('1d','13388749920')
    ['13']
    >>> re.findall('d','13388749920')
    ['1', '3', '3', '8', '8', '7', '4', '9', '9', '2', '0']
    >>> re.findall('2d','13388749920')
    ['20']
    >>> re.findall('1d{10}','13388749920')
    ['13388749920']
    >>> re.findall('1d{5}','13388749920')
    ['133887']
    >>>

    14、匹配(非)普通字符(普通字符:数字、字母、下划线)

    元字符: w    W

    匹配规则:w 匹配任意一个普通字符  [_0-9a-zA-Z]

           W 匹配任意非普通字符[^_0-9a-zA-Z]

    >>> re.findall('w','hello_world')
    ['h', 'e', 'l', 'l', 'o', '_', 'w', 'o', 'r', 'l', 'd']
    >>> re.findall('w+','hello_world')
    ['hello_world']
    >>> re.findall('w+','hello_!world')             ! 为非普通字符
    ['hello_', 'world']
    >>> re.findall('W+','hello_!world')
    ['!']
    >>>

    15、匹配(非)空字符:

    元字符:s       S

    匹配规则:s 匹配任意空字符 【 】存在,但看不见 :表示回车

                              表示换行

                                :表示Table

         S 匹配任意非空字符

    >>> re.findall('s','helle world ')
    [' ', ' ', ' ', ' ']
    >>>

    >>> re.findall('[A-Z]S*','hello Wold ni China S ##')      *匹配0或多个
    ['Wold', 'China', 'S']
    >>> re.findall('[A-Z]S+','hello Wold ni China S ##')      +匹配1或多个
    ['Wold', 'China']
    >>>

    >>> re.findall('[A-Z]S','hello Wold ni China S ##')  因为S后面是空格,S匹配非空字符
    ['Wo', 'Ch']
    >>>

    >>> re.findall('[A-Z]','hello Wold ni China S ##')
    ['W', 'C', 'S']
    >>>

    16、匹配起止位置

    元字符:A    

    匹配规则:A 匹配开始位置

          匹配结束位置

    绝对匹配  Aabc-------------abc(且字符串只是abc)

    >>> re.findall('A/w+/w+','/car/byd')
    ['/car/byd']
    >>>

    17、匹配(非)单词边界位置

    元字符:    B

    匹配规则: 匹配单词的边界

         B 匹配非单词的边界

    单词边界:数字、字母、下划线和其他字符的交接位置为单词的边界

    >>> re.findall(r"Bis","This is china")        注意此次有r,作为转移符
    ['is']
    >>> re.findall(r"is","This is china")
    ['is']
    >>> re.findall(r"is","This is china")
    ['is', 'is']
    >>>

    总结:

    匹配单一一个字符:a  .  d D W w s S [...]   [^...]

    匹配重复性:*匹配0次或多次

          +匹配1次或多次

          ?匹配0次或1 次

          {N} 匹配n次

          {m,n}匹配m到n次

    匹配某个位置:^ & A   B

    其他:|  () 

    转义字符

    正则表达式特殊符号:

    . *  ? $ ''" '' [] {} () ^

    如果想匹配特殊符号则加转义

    >>> re.findall(""."",'this is a "."')

    ['"."']

    r(raw 原生字符串,不进行转义)

    贪婪和非贪婪

    正则表达式默认的重复匹配模式:

    贪婪模式(尽快多的向后匹配)

    >>> re.findall("bc*",'thbcccccccs')
    ['bccccccc']
    >>> re.findall("bc+",'thbcccccccs')
    ['bccccccc']
    >>> re.findall("bc?",'thbcccccccs') 0次或1次
    ['bc']

    >>> re.findall("bc{2,5}",'thbcccccccs')
    ['bccccc']

    * +?{m,n}

    非贪婪模式(尽可能少的匹配内容,满足正则表达式含义即可)

    * +?{m,n}  在这4个字符后,加上?就可以

    >>> re.findall("bc*?",'thbcccccccs')
    ['b']
    >>> re.findall("bc+?",'thbcccccccs')
    ['bc']
    >>> re.findall("bc{2,5}?",'thbcccccccs')
    ['bcc']
    >>> re.findall("bc??",'thbcccccccs')
    ['b']
    >>>

    正则表达式分组

    使用()可以为一个正则表达式建立一个子组,子组可以看做内部的整体

    子组的作用:

    1、增加子组后,对正则表达式整体的匹配内容没有影响

    >>> re.findall("aa(fc)",'thaafcccccccs')
    ['fc']
    >>> re.findall("ax(fc)",'thaafcccccccs')
    []
    >>>

    2、子组可以改变重复元字符的重复行为

    >>> re.findall("aa(fc)*",'thaafcccccccs')
    ['fc']
    >>> re.findall("aafc*",'thaafcccccccs')
    ['aafccccccc']
    >>>

    3、子组在某些操作中,可以对子组匹配内容单独提取

    子组的注意事项

    1、每个正则表达式可以有多个子组,由外到内,由左到右

    2、子组通常不要交叉

    捕获组和非捕获组(命名组和非命名组)

    子组命名格式

    (?P<name>abc)

    1、很多编程接口可以直接通过名字获取子组匹配内容

    2、捕获组中的正则表达式可以通过名字重复调用

    (?P=name)

    >>> re.search('(?P<dog>ab)cdef(?P=dog)','abcdefabcx').group()
    'abcdefab'
    >>>

    re模块 

    compile(pattern ,flags=0)

    功能:获取正则表达式对象

    参数:pattern:正则表达式

       flags:功能标志位,提供更丰富的匹配

    返回值:正则表达式对象

    obj.findall(string,pos,endpos)

    功能:通过正则表达式匹配字符串

    参数:string 目标字符串

       pos 目标字符串的匹配开始位置

       endpos 目标字符串的结束位置

    返回值: 匹配到的所有内容以列表返回

    *如果正则表达式有子组,则只显示子组匹配内容

    import re
    
    pattern=r'ab'
    
    #获取正则表达式
    obj=re.compile(pattern)
    l=obj.findall('abcdeabdabab')
    print(l)
    

     结果

    yangrui@ubuntu:~/day9$ python3 re1.py 
    ['ab', 'ab', 'ab', 'ab']
    

      

    import re
    
    pattern=r'ab'
    
    #获取正则表达式
    obj=re.compile(pattern)
    l=obj.findall('abcdeabdabab',6,10)
    print(l)
    

      结果

    yangrui@ubuntu:~/day9$ python3 re1.py 
    ['ab']
    

      

    obj.split(string)

    功能:按照正则表达式切割目标字符串

    参数;目标字符串

    pattern=r's+'
    
    #获取正则表达式
    obj=re.compile(pattern)
    
    l=obj.split('hello world hello bj !')
    print(l)

    yangrui@ubuntu:~/day9$ python3 re1.py
    ['hello', 'world', 'hello', 'bj', '!']

    obj.sub(replacestr,string,max)

    功能:替换正则表达式匹配到的内容

    参数:replacestr 要替换的内容

        string 目标字符串

        max 最多替换几处

    返回值:替换后字符串

    import re
    
    pattern=r's+'
    
    #获取正则表达式
    obj=re.compile(pattern)
    
    s=obj.sub('##',"hello world come on !")
    print(s)

    yangrui@ubuntu:~/day9$ python3 re1.py
    hello##world##come##on##! 

    import re
    
    pattern=r's+'
    
    #获取正则表达式
    obj=re.compile(pattern)
    
    s=obj.sub('##',"hello world come on !",3)
    print(s) 

    yangrui@ubuntu:~/day9$ python3 re1.py
    hello##world##come##on !

    obj.subn(replacestr,string,count)

    功能:替换正则表达式匹配到的内容

    参数:replacestr 要替换的内容

        string 目标字符串

        count 最多替换几处

    返回值:返回替换后的字符串和实际替换的个数

    import re
    pattern=r's+'
    #获取正则表达式
    obj=re.compile(pattern)
    
    s=obj.subn('##',"hello world come on !")
    print(s) 

    yangrui@ubuntu:~/day9$ python3 re1.py
    ('hello##world##come##on##!', 4)    4是替换的个数

    obj.finditer(string)

    功能:使用正则表达式匹配目标内容

    参数:目标字符串

    返回值:迭代对象,迭代的每个内容为一个match对象

    import re
    tt=re.finditer(r'd+',
    '我爱你祖国,520我爱你00轻松气质,红梅2019')
    for i in tt:
    print(i.group())

    yangrui@ubuntu:~/day9$ python3 re1.py
    520
    00
    2019

    re.match(pattern,string)

    功能:匹配一个匹配字符的开头

    参数:目标字符串

    返回值:如果匹配到返回  match obj

        没有匹配到返回 None

  • 相关阅读:
    Object类入门这一篇就够了!
    什么是Java内部类?
    Spring Cloud Alibaba基础教程:支持的几种服务消费方式(RestTemplate、WebClient、Feign)
    Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现
    Spring Cloud Stream同一通道根据消息内容分发不同的消费逻辑
    Spring Cloud Stream消费失败后的处理策略(四):重新入队(RabbitMQ)
    Spring Cloud Stream消费失败后的处理策略(三):使用DLQ队列(RabbitMQ)
    Spring Cloud Stream消费失败后的处理策略(二):自定义错误处理逻辑
    Spring Cloud Stream消费失败后的处理策略(一):自动重试
    Spring Cloud Stream如何消费自己生产的消息?
  • 原文地址:https://www.cnblogs.com/sike8/p/11236065.html
Copyright © 2011-2022 走看看