"""
RE是什么
正则 表达 式子
就是一些带有特殊含义的符号或者符号的组合
它的作用是对字符串进行过滤
在一堆字符串中找到你所关心的内容
你就需要告诉计算机你的过滤规则是什么样 通过什么方式来告诉计算机 就通过正则表达式
第一步 学习正则表达式 各种符号所表示的含义
re模块的内部实现 不是python 而是调用了c库

"""

import re

# 待处理字符串
src = "abcdef \t 12121gaa1a _ - a * / \n a"
# 在字符串中查找所有满足条件的
# print(re.findall("ab",src))

# \w字母数字下划线 \W非字母数字下划线 与前面相反
# print(re.findall("\w",src))
# print(re.findall("\W",src))

# \s 所有不可见字符 \S 可见字符
# print(re.findall("\s",src))
# print(re.findall("\S",src))

# \d 所有数字 \D所有非数字
# print(re.findall("\d",src))
# print(re.findall("\d",src))

# 特殊字符直接匹配
# print(re.findall("\n",src))
# print(re.findall("\t",src))

# . 除了\n以外任意字符
# print(re.findall(".",src))
#
# print(re.findall("\d","abcdef \t 12121gaa1a _ - a * / \n a"))

# \s \w \d . 都是匹配单个字符
# 匹配重复字符 * + ? {}
# * 前面的表达式出现任意次
# print(re.findall("\w\d*","1 12 aa"))

# +重复1次或多次
# print(re.findall("\d+","1 12121272163871asas6378232678a aa"))

# ?表示重复0次或1次
# print(re.findall("\d?","aa bb a1c 1c1 123"))

# {m,n} 最少m次 最多n次
# print(re.findall("\d{1,3}","1 12 123 1234 "))

# {m} 必须是m次
print(re.findall("[a-z]{2}","a aa aaa ssss"))

# {,m} 最大m次 0-m
print(re.findall("[a-z]{,3}","a aa aaa a1aa"))

# 从字符串1982asasa中找到所有0 1 2

# 匹配范围
# | 0|1|2 或
# print(re.findall("0|1|2","1982asasa"))

# [] 字符集合 括号内的符号不是整体
# print(re.findall("[012]","1982asasa"))
# ============================================ 在范围匹配时使用脱字符表示取反
# 在这里表示除了0-9之外的任意字符
# print(re.findall("[^012]","1982asasa"))

# 请找出 所有的数字0-9和字母a-z A-Z 注意 减号只有在两个字符中间才有范围的意思 在两边都是普通字符
# print(re.findall("[0-9a-zA-Z]","1982asa+sa"))

# ^ 匹配行首
# print(re.findall("^h","hellohello"))
#
# # $ 匹配行未 注意写在表达式后面
# print(re.findall("[a-z]oh$","lohhel9ohelloh"))

# 单词边界 指的是单词的末尾
print(re.findall("h\\b","helloh hih"))

print(re.findall("h\\B","hellhoh hih"))

# 双斜杠??
print(re.findall("a\\\\c","aakakja\c"))

# 贪婪匹配 * + 不是固定的特殊符号 只是一种现象
# 会一直匹配到不满足条件为止 用问号来阻止贪婪匹配(匹配最少满足条件的字符数)
print(re.findall("\w+?","ajshsjkdsd"))

print(re.findall("\w*?","ajshsjkdsd"))

# 说明时候需要阻止贪婪

src = "<img src='www.baidupic.shuaiqi1.jpg'><img src='www.baidupic.shuaiqi2.jpg'><img src='www.baidupic.shuaiqi3.jpg'>"

# () 用于给正则表达式分组(group) 不会改变原来的表达式逻辑意义
# 效果 优先取出括号内的内容

# 请用正则表达式取图片地址
print(re.findall("src='(.+?)'",src))

# 了解 加上?: 可以取消括号中的优先级
print(re.findall("src='(?:.+?)'",src))
print(re.findall("src='(?:www).(?:baidupic)",src))

"""
re模块常用方法
findall 从左往右查找所有满足条件的字符 返回一个列表
search 返回第一个匹配的字符串 结果封装为对象 span=(0, 5) 匹配的位置 match匹配的值
match 匹配行首 返回值与search相同
对于search match 匹配的结果通过group来获取
compile 将正则表达式 封装为一个正则对象 好处是可以重复使用这个表达式

"""
import re
print(re.search("hello"," world hello python"))
print(re.match("hello"," world hello python"))
print(re.split("hello","world hello python",maxsplit=0))

#
pattern = re.compile("hello")
# print(pattern.search("world hello python",3))

# print(re.sub("hello","gun","world hello Java"))

# 现有字符串如下
src = "c++|java|python|shell"
# 用正则表达式将c 和shell换位置
# print(re.sub("([a-zA-Z]+)|","",src))

# 先用分组将 内容 分为三个 1.c++ 2.|java|python| 3.shell
print(re.findall("(.+?)(\|.+\|)(.+)",src))

print(re.sub("(.+?)(\|.+\|)(.+)",r"\3\2\1",src))

# print(re.search("(.+?)(\|.+)\|(.+)",src).group(3))
# print(re.sub("(.+)(\|.+\|)(.+)",r"\3\2\1",src))
# print(re.search("(.+?)\|",src))

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

作用:用于执行系统命令

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

总结 subprocess的好处是可以获取指令的执行结果
subprocess执行指令时 可以在子进程中 这样避免造成主进程卡死
注意 管道的read方法和文件的read有相同的问题 read后光标会到文件末尾 导致第二次无法read到数据

re模块,subprocess模块的更多相关文章

  1. s14 第5天 时间模块 随机模块 String模块 shutil模块(文件操作) 文件压缩(zipfile和tarfile)shelve模块 XML模块 ConfigParser配置文件操作模块 hashlib散列模块 Subprocess模块(调用shell) logging模块 正则表达式模块 r字符串和转译

    时间模块 time datatime time.clock(2.7) time.process_time(3.3) 测量处理器运算时间,不包括sleep时间 time.altzone 返回与UTC时间 ...

  2. Python全栈之路----常用模块----subprocess模块

    我们经常需要通过Python去执行一条系统命令或脚本,系统的shell命令是独立于你的python进程之外的,每执行一条命令,就是发起一个新进程,通过python调用系统命令或脚本的模块在python ...

  3. os模块,os.path模块,subprocess模块,configparser模块,shutil模块

    1.os模块 os表示操作系统该模块主要用来处理与操作系统相关的操作最常用的文件操作打开 读入 写入 删除 复制 重命名 os.getcwd() 获取当前执行文件所在的文件夹路径os.chdir(&q ...

  4. os模块-subprocess 模块- configpaser 模块

    一. os 模块 主要用于处理与操作系统相关操作,最常用文件操作 使用场景:当需要操作文件及文件夹(增,删,查,改) os.getcwd()  获取当前工作目录 os.chdir('dirname') ...

  5. python hashlib模块 logging模块 subprocess模块

    一 hashlib模块 import hashlib md5=hashlib.md5() #可以传参,加盐处理 print(md5) md5.update(b'alex') #update参数必须是b ...

  6. Python re模块 subprocess模块

    re模块 内部实现不是Python 而是调用了c的库 re是什么 正则 表达 式子 就是一些带有特殊含义的符号或者符号的组合作用: 对字符串进行过滤 在一对字符串中找到所关心的内容 你就需要告诉计算机 ...

  7. python实现系统调用cmd命令的模块---subprocess模块

    如果要python实现系统命令或者调用脚本,python中可以利用os或者subprocess模块实现: 一.os模块: # coding:utf-8 command = os.system('net ...

  8. python day 9: xlm模块,configparser模块,shutil模块,subprocess模块,logging模块,迭代器与生成器,反射

    目录 python day 9 1. xml模块 1.1 初识xml 1.2 遍历xml文档的指定节点 1.3 通过python手工创建xml文档 1.4 创建节点的两种方式 1.5 总结 2. co ...

  9. configparser模块 subprocess 模块,xlrd 模块(表格处理)

    今日内容: 1.configparser模块 2.subprocess模块 3.xlrd(读),xlwt(写) 表格处理 configparser模块 import configparser # co ...

随机推荐

  1. Hibernate: save, persist, update, merge, saveOrUpdate[z]

    [z]https://www.baeldung.com/hibernate-save-persist-update-merge-saveorupdate 1. Introduction In this ...

  2. jq以固定开头的class属性的名称

    $("div[class^='pick']").css({'border-color':'#000000'}); div [class^="aaa"]

  3. c#cardview 把record 去掉,控件cardview的cardCaption标题

    private void cardView1_CustomDrawCardCaption(object sender, DevExpress.XtraGrid.Views.Card.CardCapti ...

  4. nodejs 开发服务端 child_process 调试方法(1)

    由于最近正在做一个服务端项目,采用了nodejs引擎开发,主要是master-worker工作机制;主进程可以直接调试,但是子进程调试好像有点麻烦,我没有找到好的方法; worker这里,我分拆成了几 ...

  5. VS2010正则批量替换set_和get_

    批量替换set_: daohang.set_ChannelName(rowArray[0]["ChannelName"].ToString()); daohang.set_Chan ...

  6. Maximum Average Subarray II LT644

    Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...

  7. 阿里云安骑士-Centos7系统基线合规检测-修复记录

    执行命令 sysctl -w net.ipv4.conf.all.send_redirects=0sysctl -w net.ipv4.conf.default.send_redirects=0sys ...

  8. CXF wsdl2java (转载)

    2011-03-28 14:27 9735人阅读 评论(2) 收藏 举报 servicewebserviceinterfacejavastringserver CXF wsdl2Java 一.  简介 ...

  9. UD系统主定制界面

  10. R语言2版本3版本安装

    ./configure --prefix=/YZpath/public/software/R/R-3.5.0 --with-readline=no --with-x=no make make inst ...