re模块与subprocess模块介绍
一:re模块 处理正则表达式的模块,正则表达式就是一些带有特殊含义的符号或者符号的组合。
作用:对字符串进行过滤,在一堆字符串中找到你所关心的内容,你就需要告诉计算机你的过滤的
规则是什么样的,通过什么方式来告诉计算机,就通过正则表达式。
正则表达式的各种符号所表示的含义 (注:re模块的内部实现不是python,而是调用了c库)
举例说明:
import re
src='abc_defa 34_h\na'
print(re.findall('a',src)) # ['a', 'a', 'a']
print(re.findall('\w',src)) # \w匹配字母数字下划线 ['a', 'b', 'c', '_', 'd', 'e', 'f', 'a', '3', '4', '_', 'h', 'a']
print(re.findall('\W',src)) # \W匹配非字母数字下划线 [' ', '\n']
print(re.findall('\s',src)) # \s匹配所有不可见字符 [' ', '\n']
print(re.findall('\S',src)) # \S匹配所有可见字符 ['a', 'b', 'c', '_', 'd', 'e', 'f', 'a', '3', '4', '_', 'h', 'a']
print(re.findall('\d',src)) #\d匹配任意数字,等价于[0-9] ['3', '4']
print(re.findall('\D',src)) #\D匹配任意非数字 ['a', 'b', 'c', '_', 'd', 'e', 'f', 'a', ' ', '_', 'h', '\n', 'a']
print(re.findall('\n',src)) #\n只匹配换行符 ['\n']
print(re.findall('.',src)) # . 除了\n以外的任意字符 ['a', 'b', 'c', '_', 'd', 'e', 'f', 'a', ' ', '3', '4', '_', 'h', 'a']
像\s \w \d都是匹配单个字符,如果匹配重复字符
* + ? {}
例:
print(re.findall('\d*','1 12 aa bb')) # * 前面的表达式出现0次或任意次 ['1', '', '12', '', '', '', '', '', '', '']
print(re.findall('\d+','1 12 333 aa bb')) # + 重复1次或多次 ['1', '12', '333']
print(re.findall('\d?','aa bb a1c 1c1')) # ? 表示重复0次或1次 ['', '', '', '', '', '', '', '1', '', '', '1', '', '1', '']
print(re.findall('\d{1,3}','aa bb a1c 1c11')) # {m,n}手动指定重复次数,最少m次,最多n次 ['1', '1', '11']
print(re.findall('[a-z]{3}','a aa aaa aaaa')) #{m} 必须是m次 ['aaa', 'aaa']
print(re.findall('[a-z]{,3}','a aa aaa aaaa')) #{,m}最大m次 ['a', '', 'aa', '', 'aaa', '', 'aaa', 'a', '']
匹配范围 | []
例:
print(re.findall('1|0|2','1982asds')) # a|b 匹配a或b ['1', '2']
print(re.findall('[012]','1982asds')) #[abc] 用来表示一组字符,单独列出,[abc] 匹配‘a’ 'b'或‘c’ ['1', '2']
例子:找出所有的数字和字母 (注:减号只有在两个字符中间才有范围的意思,在两边都是普通字符)
print(re.findall('[0-9a-zA-Z]','1982asds')) #['1', '9', '8', '2', 'a', 's', 'd', 's']
匹配行首,在范围匹配时使用^字符可以表示取反 ^
例:print(re.findall('^h','hellohelloh')) # ^ 行首 ['h']
print(re.findall('[^0-9a-zA-Z]','1982asds')) # ^ 取反 []
匹配行尾:$ 需要写在表达式后面
例: print(re.findall('oh$','eololheullooh')) # $ 行尾 ['oh']
单词边界:\b 也就是指单词和空格间的位置(也就是单词末尾)
例: print(re.findall(r'h\b','hello world hih okh')) # ['h', 'h']
小练习:
1:验证密码是否符合规则:不能少于8位,只能是数字字母下划线,最长16位
import re
pwd1='123456789'
pwd2='1234'
print(re.findall('\w{8,16}',pwd)) #pwd1 ['12345678'] , pwd2 []
2:验证手机号码:长度11,全是数字,前三位固定范围[189 131 150]
import re
phone='13162996258'
print(re.findall('1(?:89|31|50)\d{8}',phone))
贪婪匹配 * + 不是固定的特殊符号,只是一种现象
例: print(re.findall('\w','ajshskhkcd')) #['a', 'j', 's', 'h', 's', 'k', 'h', 'k', 'c', 'd']
print(re.findall('\w+','ajshskhkcd')) #['ajshskhkcd']
print(re.findall('\w*','ajshskhkcd')) #['ajshskhkcd', '']
会一直匹配到不满足条件为止,用?来阻止贪婪匹配 (匹配最少满足条件的字符数)
print(re.findall('\w+?','ajshskhkcd')) #['a', 'j', 's', 'h', 's', 'k', 'h', 'k', 'c', 'd']
print(re.findall('\w*?','ajshskhkcd')) # ['', 'a', '', 'j', '', 's', '', 'h', '', 's', '', 'k', '', 'h', '', 'k', '', 'c', '', 'd', '']
什么时候需要阻止贪婪匹配
例:src="<img src='www.baidupic.shuai1.jpg'><img src='www.baidupic.shuai2.jpg'><img src='www.baidupic.shuai3.jpg'>"
正则表达式取出图片地址
print(re.findall("src='.+?'",src))
#["src='www.baidupic.shuai1.jpg'", "src='www.baidupic.shuai2.jpg'", "src='www.baidupic.shuai3.jpg'"]
print(re.findall("src='(.+?)'",src))
#['www.baidupic.shuai1.jpg', 'www.baidupic.shuai2.jpg', 'www.baidupic.shuai3.jpg']
print(re.findall("src='(?:.+?)'",src))
#["src='www.baidupic.shuai1.jpg'", "src='www.baidupic.shuai2.jpg'", "src='www.baidupic.shuai3.jpg'"]
()用于给正则表达式分组(group),不会改变原来的表达式逻辑意义,效果:优先取出括号内的内容
re模块的常用方法:
1:findall 从左往右查找所有满足条件的字符 ,返回一个列表
2: search 返回第一个匹配的字符串 ,结果封装为对象
span=(0, 5) 匹配的位置 match匹配的值
例:
print(re.search('hello','hello world hello ython')) #<re.Match object; span=(0, 5), match='hello'>
print(re.search('hello','hello world hello ython').group()) # hello
3:match 匹配行首 ,返回值与search相同
例:print(re.match('hello','hello world hello ython')) #<re.Match object; span=(0, 5), match='hello'>
print(re.match('hello','hello world hello ython').group()) # hello
对于search ,match 匹配的结果通过group来获取
4:split 分割
例:print(re.split('hello','world hello ython',maxsplit=0)) # ['world ', ' ython']
5:compile 将正则表达式封装为一个正则对象,好处是可以重复使用这个表达式
例:pattern=re.compile('hello')
print(pattern.search('hello world hello ython')) #<re.Match object; span=(0, 5), match='hello'>
6:sub 替换
例:print(re.sub('hello','hao','world hello ython')) #world hao ython
小练习:现有如下字符串,用正则表达式将c和shell换位置
src='c|java|ython|shell'
#先分3组
print(re.findall('(.+?)(\|.+\|)(.+)',src)) # [('c', '|java|ython|', 'shell')]
#再替换
print(re.sub('(.+?)(\|.+\|)(.+)',r'\3\2\1',src)) # shell|java|ython|c
============================================================
二:subprocess模块
sub 子
process 进程
什么是进程:正在进行中的程序,每当打开一个程序就会开启一个进程,每个进程包含运行程序所需的所有
资源,正常情况下不可以跨进程访问数据,但是有些情况就需要访问别的进程数据,就提供一
个叫做管道的对象,专门用于跨进程通讯。
作用:用于执行系统命令
常用方法:
1:run 返回一个表示执行结果的对象
2: call 返回的执行的状态码
3: Popen 返回的也是对象 ① stdout
②stderr
③stdin
总结:subprocess的好处是可以获取指令的执行结果
subprocess执行指令时可以在子进程中执行,这样避免造成主进程卡死
re模块与subprocess模块介绍的更多相关文章
- python重要模块之subprocess模块
python重要模块之subprocess模块 我们经常要通过python去执行系统的命令或者脚本,系统的shell命令是独立于你的python进程之外的,每执行一条命令,就相当于发起了一个新的进程, ...
- configparser模块,subprocess 模块,xlrd,xlwt ,xml 模块,面向对象
1. configparser模块 2.subprocess 模块 3.xlrd,xlwt 4.xml 模块 5.面向对象 面向对象是什么? 是一种编程思想,指导你如何更好的编写代码 关注点在对象 具 ...
- [xml模块、hashlib模块、subprocess模块、os与sys模块、configparser模块]
[xml模块.hashlib模块.subprocess模块.os与sys模块.configparser模块] xml模块 XML:全称 可扩展标记语言,为了能够在不同的平台间继续数据的交换,使交换的数 ...
- python-re模块和subprocess模块
一.re模块 re中文为正则表达式,是字符串处理的常用工具,通常用来检索和替换符合某个模式的文本. 注:要搜索的模式和字符串都可以是unicode字符串(str)和8位字符串(bytes),但是不能将 ...
- os模块、os.path模块、shutil模块、configparser模块、subprocess模块
一.os模块 os指的是操作系统 该模块主要用于处理与操作系统相关的操作,常用的是文件操作(读.写.删.复制.重命名). os.getcwd() 获取当前文件所在的文件夹路径 os.chdir() ...
- 模块讲解----subprocess模块
历史 #输出结果到屏幕上,并不返回执行状态os.system('dir')#保存命令的执行结果输出ret = os.popen('dir').read() 问题:上面2条是把命令结果保存下来了,但是返 ...
- Python开发基础-Day15正则表达式爬虫应用,configparser模块和subprocess模块
正则表达式爬虫应用(校花网) import requests import re import json #定义函数返回网页的字符串信息 def getPage_str(url): page_stri ...
- python16_day06【类、RE模块、subprocess模块、xml模块、shelve模块】
一.shelve模块 import shelve # 基于pickle模块, d = shelve.open('shelve_test') class Test(object): def __init ...
- python基础之正则表达式爬虫应用,configparser模块和subprocess模块
正则表达式爬虫应用(校花网) 1 import requests 2 import re 3 import json 4 #定义函数返回网页的字符串信息 5 def getPage_str(url): ...
随机推荐
- 【JavaScript 6连载】一、关于对象(访问)
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 校正PHP服务器时间不准的问题
关于怎样解决PHP服务器时间不准的问题,得针对不同的情况进行不同的处理. 下面是经常遇到的情况,及应对办法. 1.PHP服务器时区不对,使用下面代码修正: <?php $timezone = & ...
- Porsche Piwis Tester 2 Online Coding Guide
Porsche Piwis online programming account service is piwis porsche subscription and piwis tester 2 on ...
- 一位前辈的博客,收获颇丰,包括Android、Java、linux、前端、大数据、网络安全等等
https://www.cnblogs.com/lr393993507/ 魔流剑
- onclick或者其他事件在部分移动端无效的问题
最近开发碰到一个问题,大多数手机都可以正常访问点击,但是有部分手机onclick无效,不知道可能是什么原因?该如何解决? 我遇到的这个问题,实际不是onclick的原因,而是因为js里面包含es6的语 ...
- 在配置好环境以后,启动tomcat后,出现这个异常
15-Apr-2019 16:48:13.299 严重 [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.StandardConte ...
- django 把函数装饰器变为方法装饰器
暗暗啊
- yii2项目中运行composer 过程中遇到的问题
问题1: Your requirements could not be resolved to an installable set of packages 则表明 未安装fxp/composer-a ...
- 两步完成ssh免密码登录
1.生成公钥/私钥 ssh-keygen -N '' 生成公钥在/root/.ssh目录下. 2.分发公钥 ssh-copy-id root@192.168.137.141 192.168.137.1 ...
- JS实现复制页面文字弹出消息提醒
先上效果图: 简洁版: <script type="text/javascript"> document.body.oncopy=function(){ alert(& ...