zoukankan      html  css  js  c++  java
  • re的剩余模块和subprocess模块

      今天学习了re的剩余模块和subprocess模块

      一、re的剩余模块

        #[]  范围匹配 中间 用-来连接

        re.findall('[a-zA-Z0-9]','a ab asdf sadf asdf 213 asfd ')

        如果要匹配 符号- 要写表达式的左边或右边

        print(re.findall('[-ab]','asdf dsf sdfa asf- asf---'))

        重复匹配  表达式的匹配次数

        * 表示 任意次数  所以0次也满足

        print(re.findall('[a-zA-Z]*','a ab abc asdfasdfasdf a123xc'))

        + 一次或多次

        print(re.findall('[a-zA-Z]+','a b abc adsfasdfasd a123c'))

        ? 0次或1次

        print(re.findall('[a-zA-Z]?','a ab abc asdfasdfasdf a123c'))

        {1,2} 自定义匹配次数  {1,} 1到无穷  {,1} 0到1次

        print(re.findall('[a-zA-Z]{1,3}','a b abc weqrsdafasdfasd a123c'))

        + * 贪婪匹配  表达式匹配的情况下  尽可能的多拿       (一直匹配 直到不满足为止)

        print(re.findall('w*','asdfsdfsadfasd asdfsdfasd'))

        print(re.findall('w+','sdfasdfasd asdfsdfas'))

        非贪婪匹配 在表达式的后面加上?

        print(re.findall('w?','sdfsadfasd asdfasfasd')) 非贪婪匹配

        分组  加上分组 不会改变原来的规则  仅仅是将括号中的内容单独拿出来了

        print(re.findall('([a-zA-Z]+)_dsb','aigen_dsb cxx_dsb alex_dsb zxx_dsb _dsb'))

        re模块中常用的函数

        match 从字符串开始处匹配  只找一个

        print(re.match('w*','abc').group(0))  获取匹配成功的内容

        group 用来获取某个分组的内容  默认获取第0组  就是整个表达式本身

        print(re.match('([a-zA-Z]+)(_dsb'),'aigen_dsb cxx_dsb alex_dsb zxx_dsb _dsb').group(2))

        print(re.match('w*','abc').span())  获取匹配成功的内容的索引

        

        print(re.search('w*','abc').group())

        从全文范围取一个

        print(re.search('([a-zA-Z]+)(_dsb)','xxx aigen_dsb cxx_dsb alex_dsb zxx_dsb _dsb'))

        从开始的位置开始匹配

        print(re.match('([a-zA-Z]+)(-dsb)','xxx aigen_dsb cxx_dsb alex_dsb zxx_dsb _dsb').group())

        将正则表达式 编译成一个对象 往后可以不用再写表达式  直接开始匹配

        print(re.compile('w*').findall('abcd'))

        print(re.split('|_*|','python|____|js|____|java'))

        

        替换

        print(re.sub('python','PYTHON','js|python|java'))

        用正则表达式来交换位置

        text = 'java|C++|js|C|python'

        将整个内容分为三块  java  |C++|js|C|   python

        patter = '(.+?)(|.+|)(.+)'

        ?:用于取消分组  就和没写括号一样

        patter = '(?:.+?)(|.+|)(.+)'

        print(re.search(partten,text).group(0))

        print(re.sub(partten,r'231',text))

        当要匹配的内容包含时

        text = 'ap'

        'p'

        print(text)

        print(re.findall(r'a\p',text))

      二、subprocess模块

        cmd = r'dir D:上海python全栈4期day23' | findstr "py"'

        res = subprocess.Popen(cmd,shell = True,stdout = subprocess.PIPE,stderr = subprocess.PIPE)

        从管道中读取数据  管道就是  两个进程通讯的媒介

        print(type(res.stdout.read().decode('GBK')))

        print(res.stdout.read().decode('GBK'))

        print(res.stderr.read().decode('GBK'))

        

        dir = r'dir D:上海python全栈4期day23'

        find = 'findstr "py"'

        stdout  输出管道

        stdin 输入管道

        stderr  错误管道

        res1 = subprocess.Popen(dir,shell=True,stdout = subprocess.PIPE,stderr = subprocess.PIPE)

        res2 = subprocess.Popen(find,shell = True,stdout = subprocess.PIPE,stderr = subprocess.PIPE,stdin = res1.stdout)

        从管道中读取数据  管道就是  两个进程通讯的媒介

        print(type(res.stdout.read().decode('GBK')))

        print(res1.stdout.read().decode('GBK'))

        print(res2.stderr.read().decode('GBK'),'33333')

        简单总结  subprocess  主要用于执行系统指令  (启动子进程) 与os.system的不同在于

        subprocess 可以与这个字进程进行数据交换

        

  • 相关阅读:
    P4868 天天和不可描述
    天天寄快递
    iOS开发资源网站
    找图标的网址
    json解析网址
    屏幕适配
    加密 解密
    json解析网址
    IOS设计模式之三:MVC模式
    什么是Cocoa?什么是Xcode?什么是Framework?
  • 原文地址:https://www.cnblogs.com/xiaocaiyang/p/9819945.html
Copyright © 2011-2022 走看看