day 17 re模块
RE模块
import re
对一个大篇幅的字符串,按照你的规则找出想要的字符串
# 单个字符匹配
import re
# \w 与 \W #字母数字下划线, 非
# print(re.findall('\w', '太白jx 12*() _')) # ['太', '白', 'j', 'x', '1', '2', '_']
# print(re.findall('\W', '太白jx 12*() _')) # [' ', '*', '(', ')', ' '] # \s 与\S space 非space
# print(re.findall('\s','太白barry*(_ \t \n')) # [' ', '\t', ' ', '\n']
# print(re.findall('\S','太白barry*(_ \t \n')) # ['太', '白', 'b', 'a', 'r', 'r', 'y', '*', '(', '_'] # \d 与 \D 数字 非数字
# print(re.findall('\d','1234567890 alex *(_')) # ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
# print(re.findall('\D','1234567890 alex *(_')) # [' ', 'a', 'l', 'e', 'x', ' ', '*', '(', '_'] # \A 与 ^ 行首
# print(re.findall('\Ahel','hello 太白金星 -_- 666')) # ['hel']
# print(re.findall('^hel','hello 太白金星 -_- 666')) # ['hel'] # \Z 与 $ 行尾
# print(re.findall('666\Z','hello 太白金星 *-_-* \n666')) # ['666']
# print(re.findall('666\Z','hello 太白金星 *-_-* \n666')) # []
# print(re.findall('666$','hello 太白金星 *-_-* \n666')) # ['666'] # \n 与 \t
# print(re.findall('\n','hello \n 太白金星 \t*-_-*\t \n666')) # ['\n', '\n']
# print(re.findall('\t','hello \n 太白金星 \t*-_-*\t \n666')) # ['\t', '\t']
重复匹配 : [. ? * + {m,n} .* .*?]
. 匹配任意字符,除了换行符,当re.DOTALL标记被指定时,则可以匹配包括换行符的任意字符
? 匹配0个或者1个左边的字符,非贪婪方式
* 匹配0个或者多个左边的字符
+ 匹配一个或者多个左边的字符
{m,n} 匹配n到m次由前面的正则表达式定义的片段,贪婪方式
.* 匹配任意字符任意个数,贪婪模式
.*? 匹配任意字符任意个数,非贪婪模式
.匹配任意字符
print(re.findall('a.b','babb'))
print(re.findall('a.b','abb a%b a1b aab a7b a+b a b a\nb'))
print(re.findall('a.b','abb a%b a1b aab a7b a+b a b a\nb',re.DOTALL))
print(re.findall('a.bb','abb abb aaab')) ? 匹配0个或者1个由左边字符定义的片段
print(re.findall('a?b','abbzab abb aab')) # [ab b ab ab b ab]
print(re.findall('a?b','ab')) # [ab] * 匹配0个或者多个左边字符表达式
print(re.findall('a*b','b ab aaaaaab abbbbbbb'))
['b', 'ab', 'aaaaaab', 'ab', 'b', 'b', 'b', 'b', 'b', 'b'] + 匹配1个或者多个左边字符表达式
print(re.findall('a+b','b ab aaaaaab abb')) # ['ab', 'aaaaaab', 'ab'] {m,n} 匹配m个至n个左边字符表达式
print(re.findall('a{1,3}b','aaab ab aab abbb aaz aabb')) .* 贪婪匹配 从头到尾.
print(re.findall('a.*b','aab ab aaaaab a!!!@#$bz')) # [aab aaaaab a!!!@#$b] # .*? 非贪婪匹配 从头到尾.
print(re.findall('a.*?b','aab ab aaaaab a!!!@#$bz')) #
[ ]
[]
print(re.findall('a[abc]b','aab abb acb'))
print(re.findall('a[0-9]b','a1b a2b a3b acb ayb'))
print(re.findall('a[a-z]b','a1b a2b a3b acb ayb adb'))
print(re.findall('a[A-Z]b','a1b a2b a3b aAb aDb aYb'))
print(re.findall('a[a-zA-Z]b','aab aAb aWb aqb a1b')) # 大小写字母
print(re.findall('a[A-z]b','aab aAb aWb aqb a1b'))
print(re.findall('a[0-9][0-9]b','a1b a2b a29b a56b a456b'))
print(re.findall('a[-+*/]b','a+b a-b a*b a/b a6b')) 单纯的想表示- 一定要放在最前面
() 制定一个规则,将满足规则的结果匹配出来
print(re.findall('.*?_sb','wusir_sb alex_sb 日天_sb')) #['wusir_sb', ' alex_sb', ' 日天_sb']
print(re.findall('(.*?)_sb','wusir_sb alex_sb 日天_sb')) # ['wusir', ' alex', ' 日天']
print(re.findall('company|companies','Too many companies have gone bankrupt, and the next one is my company'))
print(re.findall('compan(?:y|ies)','Too many companies have gone bankrupt, and the next one is my company'))
search 只到找到第一个匹配然后返回一个包含匹配信息的对象,该对象可以通过调用group()方法得到匹配的字符串,如果字符串没有匹配,则返回None。
print(re.search('alex','re alex alex 太白'))
# print(re.search('alex','re aaaelx 太白')) # 找不到,返回None
ret = re.search('alex','re alex alex 太白') # <_sre.SRE_Match object; span=(3, 7), match='alex'>
print(ret.group()) # alex
match:行首,None,同search,不过在字符串开始处进行匹配,完全可以用search+^代替match
print(re.match('barry','barry fdlfjsadfkl ').group()) # 同样用group()取值
print(re.match('barry','qbarry fdlfjsadfkl '))
split 分割 可按照任意分割符进行分割 *****
s1 = 'wusir;太白,alex|日天!小虎'
print(re.split('[;,|!]',s1))
sub 替换
print(re.sub('barry', '太白金星', 'barry是最好的讲师,barry就是一个普通老师,请不要将barry当男神对待。',2)) # 2代表替换两个
# 太白金星是最好的讲师,太白金星就是一个普通老师,请不要将barry当男神对待
# 制定一个统一的规则
obj = re.compile('\d{2}') # 制定一个统一的匹配规则
print(obj.findall('fdsafsda4312fdsdf324'))
print(obj.findall('123fksldjf3432fdsjlkf453')) # ['12', '34', '32', '45']
finditer 迭代器
ret = re.finditer('\d', 'ds3sy4784a')
print(ret)
print(next(ret).group()) #
print(next(ret).group()) #
print([i.group() for i in ret]) # ['7', '8', '4']
命名分组
s1 = '深圳电话:0755-546123546 深圳地址:广东..'
print(re.search('\d+-\d+',s1).group())
# 命名分组
# 分组基础上定规则 ?P<组名>
ret = re.search('(?P<quhao>\d+)-(?P<num>\d+)',s1)
print(ret.group('quhao'))
print(ret.group('num'))
day 17 re模块的更多相关文章
- DAY 17常用模块
一.时间模块:time 1.时间戳:time.time() # 可以作为数据的唯一标识 print(time.time) # 1554878849.8452318 2.延迟线程的运行:time.sle ...
- Day 17 常用模块
一.时间模块:time 1.时间戳:time.time() # 可以作为数据的唯一标识 print(time.time) # 1554878849.8452318 2.延迟线程的运行:time.sle ...
- 17 hashlib模块
1.HASH的基本概念 Hash,一般翻译做“散列”,也有直接音译为”哈希”的,就是把任意长度的输入(又叫做预映射,pre-image),通过散列算法,变换成固定长度的输出,该输出就是散列值. 这种转 ...
- python标准库介绍——17 tempfile 模块详解
==tempfile 模块== [Example 2-6 #eg-2-6] 中展示的 ``tempfile`` 模块允许你快速地创建名称唯一的临时文件供使用. ====Example 2-6. 使用 ...
- SAP 已经有17个模块
SAP模块清单: 传统五大郎: MM,SD,PP,FI 财务会计CO 管理会计 +QM 质量管理 (制造业用的不少)+ABAP.BASIS.BW BI商务智能的组件之一CRM 客户管理SRM 供应商管 ...
- day 17 re模块 正则表达式
import re 引用re模块 查找 finall:匹配所有,每一项都是列表中的一个元素 search:只匹配从左到右的第一个,得到的不是直接的结果而是一个变量,通过group方法获取结果,没 ...
- Python实用笔记 (17)模块
一个abc.py的文件就是一个名字叫abc的模块,一个xyz.py的文件就是一个名字叫xyz的模块. 现在,假设我们的abc和xyz这两个模块名字与其他模块冲突了,于是我们可以通过包来组织模块,避免冲 ...
- python模块学习心得
初始模块 1.什么是模块 模块是用来实现某项功能的一大堆代码,为什么会有模块呢?过程式编程的时候为了减少程序员编程代码的重复性,就利用函数的调用减少了代码的重复性,但是某些时候程序会过于的庞大,我们会 ...
- thinkphp model模块
1.获取系统常量信息的方法:在控制器DengLuController里面下写入下面的方法,然后调用该方法. public function test() { //echo "这是测试的&qu ...
随机推荐
- Delphi中QuotedStr介绍及使用
delphi 函数给字符串两边加单引号并返回.声明:function QuotedStr(const S: string): string;用函数 QuotedStr把字符串S转换成为用引号括起来的字 ...
- 【386】operator 的 itemgetter、slice、and_、or_
itemgetter 用来获取数组中指定索引的元素 from operator import itemgetter itemgetter(1, 3, 5)('ABCDEFG') output: ('B ...
- search() 方法解析
search()方法支持正则表达式的String对象的方法. 好,我们直接来贴代码,看效果,从实践理解透析方法的知识点和实际运用. var str="Visit W3School!" ...
- 多线程 死锁 wait(int i) notifyAll()
public class ThreadDemo5 { public static void main(String[] args){ Pool pool = new Pool(); Productor ...
- Hibernate 再接触 关系映射 一对一单向外键联合主键关联
例子: Husband.java package com.bjsxt.hibernate; import javax.persistence.Entity; import javax.persiste ...
- C# 图像处理:复制屏幕到内存中,拷屏操作
/// <summary> /// 复制屏幕到内存中 /// </summary> /// <returns>返回内存流</returns> publi ...
- python批量操作Linux服务器脚本,ssh密码登录(执行命令、上传、下载)(一)
-*- paramiko.util.log_to_file( ssh = paramiko.SSHClient() ssh.set_missing ...
- MD5加密和彩虹表
首先叙述一下彩虹表的原理.本部分内容.图片和例子基本来自英文维基的Rainbow table词条(Rainbow table)——中文维基中目前(2013年10月9日)尚无对应的词条——因此对本答案中 ...
- 解压.bz2失败
报错: # tar -jxf geos-3.6.2.tar.bz2 tar (child): bzip2:无法 exec: 没有那个文件或目录tar (child): Error is not rec ...
- Building Projects with Native Code
[Building Projects with Native Code] 1.安装Node(v4.0以上).Python2.JDK(v8.0以上). 添加 JAVA_HOME环境变量,指向 JDK 的 ...