• python学习----8.14--re模块,subprocess模块


    re模块

      一、什么是正则

          正则就是用一些具有特殊含义的符号组合正在一起(称为正则表达式)来描述字符或者字符串的方法。或者说:正则就是用来秒描述一类事物的规则。在python中,它内嵌在python中,并通过re模块实现,正则表达式模式被编译成一系列的字节码,然后在由C编写的匹配引擎执行。

       二、常用匹配模式

          

    import re
    
    
    #正则匹配
    # w,W   w字母数字下划线    W非字母数字下划线  与前面相反
    print(re.findall('w','hello lgh 123'))  #['h', 'e', 'l', 'l', 'o', 'e', 'g', 'o', 'n', '1', '2', '3']
    
    print(re.findall('W','hello lgh 123'))  #[' ', ' ']
    
    print('-------------------------------------------------')
    
    #s,S    s 所有不可见字符   S 可见字符
    print(re.findall('s','hello  lgh  123'))  #[' ', ' ', ' ', ' ']
    print(re.findall('S','hello  lgh  123'))  #['h', 'e', 'l', 'l', 'o', 'e', 'g', 'o', 'n', '1', '2', '3']
    
    print('-------------------------------------------------------')
    
    #
    , 	都是空,都可以被 s匹配
    print(re.findall('s','hello 
     lgh 	 123'))  #[' ', '
    ', ' ', ' ', '	', ' ']
    
    print('-------------------------------------------------------------')
    
    #
    ,	  特殊字符直接匹配
    print(re.findall(r'
    ','hello lgh 
    123'))   #['
    ']
    print(re.findall(r'	','hello lgh 	123'))    #['	']
    
    print('---------------------------------------------------------------')
    
    #d,D   d 所有数字    D所有非数字
    print(re.findall('d','hello lgh 123'))    #['1', '2', '3']
    print(re.findall('D','heelo lgh 123'))   #['h', 'e', 'l', 'l', 'o', ' ', 'e', 'g', 'o', 'n', ' ']
    
    print('---------------------------------------------------------------')
    
    #A,  A匹配字符串开始  匹配字符串结束
    print(re.findall('Ahe','hello lgh 123'))
    print(re.findall('123','hello lgh 123'))
    
    print('-----------------------------------------------------------')
    
    #^与$
    print(re.findall('^h','hello lgh 123'))
    print(re.findall('3$','hello lgh 123'))
    
    print('---------------------------------------------------------------')
    
    
    #重复匹配
    
    #. 除了
    以外的任意字符
    print(re.findall('a.b','a1b'))  #['a1b']
    print(re.findall('a.b','a1b b*b a b aaab'))  #['a1b', 'a*b', 'a b', 'aab']
    print(re.findall('a.b','a
    b'))   #[]
    print('===============================================================')
    
    
    #*  前面的表达式出现任意次
    print(re.findall('ab*','bbbbbbb'))  #[]
    print(re.findall('ab*','a'))   #['a']
    print(re.findall('ab*','abbb'))   #['abbbb']
    print('==================================================================')
    
    #?   表示重复0次或1次
    print(re.findall('ab?','a'))   #['a']
    print(re.findall('ab?','abbb'))   #['ab']
    
    
    #pipe所有包含小数在内的数字
    print(re.findall('d+.?d*',"asdfasdf123as1.13dfa12adsf1asdf3")) #['123', '1.13', '12', '1', '3']
    
    
    #.*默认为贪婪匹配
    print(re.findall('a.*b','a1b222222222b'))    #['a1b22222222b']
    
    #.*?为非贪婪匹配,推荐使用
    print(re.findall('a.*?b','a12222b22222b'))  #['a12222b']
    
    #+重复1次或多次
    print(re.findall('ab+','a'))  #[]
    print(re.findall('ab+','abbb'))   #['abbb']
    print('==============================================================================')
    
    # {m,n} 最少m次 最多n次   {m} 必须是m次     {,m} 最大m次  0-m
    print(re.findall('ab{2}','abbb')) #['abb']
    print(re.findall('ab{2,4}','abbb')) #['abb']
    print(re.findall('ab{1,}','abbb')) #'ab{1,}' ===> 'ab+'
    print(re.findall('ab{0,}','abbb')) #'ab{0,}' ===> 'ab*'
    print('======================================================================')
    
    # |       0|1|2   或
    print(re.findall("0|1|2","1982asasa"))   #['1', '2']
    
    
    #[]  []内的都为普通字符了,且如果-没有被转意的话,应该放到[]的开头或结尾   []内的^代表的意思是取反
    print(re.findall("[012]","1982asasa"))
    print(re.findall("[^012]","1982asasa"))
    print(re.findall("[0-9a-zA-Z]","1982asa+sa"))
    print(re.findall('a[0-9]b','a1b a*b a-b a=b'))
    
    
    
    ## print(re.findall('a\c','ac')) #对于正则来说a\c确实可以匹配到ac,但是在python解释器读取a\c时,会发生转义,然后交给re去执行,所以抛出异常
    print(re.findall(r'a\c','ac')) #r代表告诉解释器使用rawstring,即原生字符串,把我们正则内的所有符号都当普通字符处理,不要转义
    print(re.findall('a\\c','ac')) #同上面的意思一样,和上面的结果一样都是['a\c']
    
    
    
    
    #():分组
    print(re.findall('ab+','ababab123')) #['ab', 'ab', 'ab']
    print(re.findall('(ab)+123','ababab123')) #['ab'],匹配到末尾的ab123中的ab
    print(re.findall('(?:ab)+123','ababab123')) #findall的结果不是匹配的全部内容,而是组内的内容,?:可以让结果为匹配的全部内容
    print(re.findall('href="(.*?)"','<a href="http://www.baidu.com">点击</a>'))#['http://www.baidu.com']
    print(re.findall('href="(?:.*?)"','<a href="http://www.baidu.com">点击</a>'))#['href="http://www.baidu.com"']
    View Code

    re模块的常用方法

      findall  从左往右查找所有满足条件的字符,返回一个列表

      search 返回第一个匹配的字符串 结果封装为对象,span=(0,5)匹配的位置  match匹配的值

      match  匹配行首  返回值与search相同

          对于search  match匹配的结果通过group来获取

      compile  将正则表达式封装为一个正则对象,好处是可以重复使用这个表达式

    print(re.findall('e','alex make love') )   #['e', 'e', 'e'],返回所有满足匹配条件的结果,放在列表里
    
    print(re.search('e','alex make love').group()) #e,只到找到第一个匹配然后返回一个包含匹配信息的对象,该对象可以通过调用group()方法得到匹配的字符串,如果字符串没有匹配,则返回None
    
    
    print(re.match('e','alex make love'))    #None,同search,不过在字符串开始处进行匹配,完全可以用search+^代替match
    
    对于search  match 匹配的结果通过group来获取
    
    
    print(re.split('[ab]','abcd'))     #['', '', 'cd'],先按'a'分割得到''和'bcd',再对''和'bcd'分别按'b'分割
    
    
    
    
    print('===>',re.sub('a','A','alex make love')) #===> Alex mAke love,不指定n,默认替换所有
    print('===>',re.sub('a','A','alex make love',1)) #===> Alex make love
    print('===>',re.sub('a','A','alex make love',2)) #===> Alex mAke love
    print('===>',re.sub('^(w+)(.*?s)(w+)(.*?s)(w+)(.*?)$',r'52341','alex make love')) #===> love make alex
    View Code

    subprocess模块

      sub  子

      process  进程 

    什么是进程
    正在进行中的程序 每当打开一个程序就会开启一个进程
    每个进程包含运行程序所需的所有资源
    正常情况下 不可以跨进程访问数据
    但是有些情况写就需要访问别的进程数据 提供一个叫做管道的对象 专门用于跨进程通讯

    作用:用于执行系统命令

    常用方法
    run 返回一个表示执行结果的对象
    call 返回的执行的状态码

    总结 subprocess的好处是可以获取指令的执行结果
    subprocess执行指令时 可以在子进程中 这样避免造成主进程卡死

     

  • 相关阅读:
    图像识别模型
    W tensorflow/core/util/ctc/ctc_loss_calculator.cc:144] No valid path found 或 loss:inf的解决方案
    CF1240F Football
    loj6537. 毒瘤题加强版再加强版
    Codeforces Global Round 9题解
    CF356E Xenia and String Problem
    CF1185G2 Playlist for Polycarp
    CF914F Substrings in a String
    Python3.5学习之旅一
    python内置数据结构性能分析
  • 原文地址:https://www.cnblogs.com/Liu-guang-hui/p/9475483.html
Copyright © 2020-2023  润新知