一:学习内容

  • 匹配单个字符与数字:.、[]、^、\d、\D、\w、\W、\s、\S
  • 匹配锚字符(边界字符):^、$、\A、\Z、\b、\B
  • 匹配多个字符:(xyz) 、x?、x*、.*、x+、x{n}、x{n,}、x{n,m}、x|y
  • 非贪婪匹配:*?、+?、??、{n,m}?

二:匹配单个字符与数字

1.点.:匹配除换行符以外的任意字符

2.[]:是字符集合,表示匹配方括号中所包含的任意一个字符

   [0123456789]   表示匹配任意一个数字
        [0-9]   表示匹配任意一个数字
        [yml]   表示匹配'y','m','l'中任意一个字符
        [a-z]   表示匹配任意一个小写字符
        [A-Z]   表示匹配任意一个大写字符
        [0-9a-zA-Z]    表示匹配任意一个数字和字母
        [0-9a-zA-Z_]    表示匹配任意一个数字、字母和下划线

3.^:是脱字符,表示不匹配集合中的字符,注意^在[]中才表示不匹配,在其他位置意义不一样

[^test]  表示匹配除了test这几个字母以外的所有字符
        [^0-9]  表示匹配所有的非数字字符

4.\d:匹配数字,效果同[0-9]

\d       表示匹配任意一个数字,这里可以不写[],直接使用\d
        [^\d]   表示匹配所有的非数字字符,效果同[^0-9]

5.\D:匹配非数字字符,效果同[^0-9]

6.\w:匹配数字、字母和下划线,效果同[0-9a-zA-Z]

7.\W:匹配非数字、字母和下划线,效果同[^0-9a-zA-Z]

8.\s:匹配任意的空白符(空格、换行、回车、换页、制表符),效果同[ \f\n\r\t]

9.\S:匹配任意的非空白符(空格、换行、回车、换页、制表符),效果同[^ \f\n\r\t]

10.举例:

print(re.search(".","tester is a good girl!"))  #.可以代表任意字符,所以匹配t,结果匹配一个t
print(re.search("[0123456789]","yml is a good gril 7"))          #匹配结果为数字7
print(re.search("[tester]","tester is a good gril 7"))                 #匹配结果为一个t
print(re.search("[^tester]","tester is a good gril 7"))               #匹配除tester之外的任意一个字符结果为空" "
print(re.search("[^0-9]","tester is a good gril 7"))                   #匹配一个非数字字符结果为t
print(re.search("\d","tester is a good gril 7"))                         #匹配结果为数字7
print(re.search("[^\d]","tester is a good gril 7"))                     #匹配结果为一个t
print(re.search("\D","tester is a good gril 7"))                        #匹配一个非数字字符结果为t
print(re.search("\w","tester is a good gril 7"))                        #匹配任意一个数字字母或下划线,结果为t

print(re.findall(".","_tester is test\nd girl 3"))                          #匹配除换行符以外的任意字符
print(re.findall(".","_tester is test\nd girl 3",flags=re.S))         #匹配包括换行符在内的任意字符

三:匹配锚字符(边界字符)

1.^:行首匹配,注意^在[]中才表示不匹配集合中的字符

2.$:行尾匹配

3.\A:匹配字符串开始,它和^区别是:

  \A只匹配整个字符串的开头即使在re.M模式下,也不会匹配其他行的行首
       ^匹配的是每行的行首

4.\Z:匹配字符串结束,它和$区别是:

  \Z只匹配整个字符串的结束即使在re.M模式下,也不会匹配其他行的行尾
       $匹配的是每行的行尾

5.\b:匹配一个单词的边界,也就是指单词和空格间的位置

6.\B:匹配非单词的边界,也就是指单词和空格间的位置

7.举例:

print(re.search("^test","test is a good girl")) #表示行首必须要是test开头
print(re.search("girl$","test is a good girl")) #表示行尾必须要是girl结尾

print(re.search("\Atest","test is a good girl"))

print(re.findall("^test","test is a good girl\ntest is a good girl",flags=re.M))
print(re.findall("\Atest","test is a good girl\ntest is a good girl",flags=re.M))

print(re.findall("girl$","test is a good girl\ntest is a good girl",flags=re.M))
print(re.findall("girl\Z","test is a good girl\ntest is a good girl",flags=re.M))

print(re.findall(r"er\b","never"))
print(re.findall(r"er\B","never"))
print(re.findall(r"er\b","nerve"))
print(re.findall(r"er\B","nerve"))

四:匹配多个字符

说明:下方的x、y、z均为假设的普通字符,n、m为非负整数,不是正则表达式的元字符

1.(xyz):匹配小括号内的内容,()内的内容xyz作为一个整体去匹配

2.x?:匹配0个或者1个x,非贪婪匹配(尽可能少的匹配)

3.x*:匹配0个或者任意多个x,贪婪匹配(尽可能多的匹配)

  .*:表示匹配0个或任意多个字符(换行符除外)

4.x+:匹配至少一个x,贪婪匹配(尽可能多的匹配)

5.x{n}:匹配确定的n个x,n是一个非负整数

6.x{n,}:匹配至少n个x,n是一个非负整数

7.x{n,m}:匹配至少n个最多m个x,注意:n <= m

8.x|y:|表示或,匹配的是x或y

9.举例:

print(re.findall(r"(test)","test"))
print(re.findall(r"y?","ytesttest"))  #匹配0个或1个
print(re.findall(r"y*","ytesttest"))  #匹配0个或任意多个,贪婪匹配
print(re.findall(r"y+","ytesttest"))  #匹配至少1个,贪婪匹配

print(re.findall(r"y{3}","ytestyyytest"))  #匹配3个y
print(re.findall(r"y{3,}","yytestyyytest"))  #匹配至少3个y
print(re.findall(r"y{3,6}","yytestyyytest"))  #匹配至少3个y,最多6个y

print(re.findall(r"(t|T)est","tttestttttest"))  #匹配t或T
print(re.findall(r"t|Test","tttestttttest"))
print(re.findall(r"((t|T)est)","tttestttttest"))

#需求:提取tester......girl
str = "tester is a good girl!tester is a nice girl!tester is a very dddd girl"
print(re.findall(r"tester.*girl",str))
print(re.findall(r"tester.*?girl",str))  #非贪婪

五:非贪婪匹配

1.  *?:前一个字符0次或无限次扩展,最小匹配

print(re.findall(r"tes*","tessssssssssssss"))
print(re.findall(r"tes*?","tessssssssssssss"))
print(re.findall(r"tess*","tessssssssssssss"))
print(re.findall(r"tess*?","tessssssssssssss"))

2.  +?:前一个字符1次或无限次扩展,最小匹配

print(re.findall(r"tes+","tessssssssssssss"))
print(re.findall(r"tes+?","tessssssssssssss"))
print(re.findall(r"tess+","tessssssssssssss"))
print(re.findall(r"tess+?","tessssssssssssss"))

3.  ??:前一个字符0次或1次扩展,最小匹配

print(re.findall(r"tes?","tessssssssssssss"))
print(re.findall(r"tes??","tessssssssssssss"))
print(re.findall(r"tess?","tessssssssssssss"))
print(re.findall(r"tess??","tessssssssssssss"))

4.  {n,m}?:扩展前一个字符n到m次,含m,最小匹配

print(re.findall(r"tes{1,2}","tessssssssssssss"))
print(re.findall(r"tesss{1,2}","tessssssssssssss"))
print(re.findall(r"tes{1,2}?","tessssssssssssss"))
print(re.findall(r"tesss{1,2}?","tessssssssssssss"))

python3笔记二十三:正则表达式之元字符的更多相关文章

  1. python3笔记二十三:正则表达式之其他函数

    一:学习内容 re.split函数 re.finditer函数 re.sub函数 group()分组 re.compile函数 二:字符串切割---re.split函数 需要导入包:import re ...

  2. python3.4学习笔记(二十三) Python调用淘宝IP库获取IP归属地返回省市运营商实例代码

    python3.4学习笔记(二十三) Python调用淘宝IP库获取IP归属地返回省市运营商实例代码 淘宝IP地址库 http://ip.taobao.com/目前提供的服务包括:1. 根据用户提供的 ...

  3. (C/C++学习笔记) 二十三. 运行时类型识别

    二十三. 运行时类型识别 ● 定义 运行时类型识别(Run-time Type Identification, RTTI) 通过RTTI, 程序能够使用基类的指针或引用来检查(check)这些指针或引 ...

  4. python3笔记二十二:正则表达式之函数

    一:学习内容 re.match函数 re.search函数 re.findall函数 二:re.match函数 需要导入包:import re 1.格式:match(pattern,string,fl ...

  5. 【Python3 爬虫】08_正则表达式(元字符与语法)

    元字符表 符号 说明 示例 . 表示任意字符 'abc'  >>>'a.c'   >>>结果为:'abc' ^ 表示字符开头 'abc'  >>> ...

  6. Java基础学习笔记二十三 Java核心语法之反射

    类加载器 类的加载 当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载,链接,初始化三步来实现对这个类进行初始化. 加载就是指将class文件读入内存,并为之创建一个Class对象.任 ...

  7. angular学习笔记(二十三)-$http(1)-api

    之前说到的$http.get和$http.post,都是基于$http的快捷方式.下面来说说完整的$http: $http(config) $http接受一个json格式的参数config: conf ...

  8. python3笔记二十四:Mysql数据库操作命令

    一:学习内容 Mysql操作命令:启动服务.停止服务.连接数据库.退出数据库.查看版本.显示当前时间.远程连接 数据库操作命令:创建数据库.删除数据库.切换数据库.查看当前选择的数据库 表操作命令:查 ...

  9. python3笔记二十一:时间操作datetime和calendar

    一:学习内容 datetime calendar 二:datetime 1.模块说明:可以理解为datetime基于time进行了封装,提供了各种使用的函数,datetime模块的接口更直接,更容易调 ...

随机推荐

  1. mybatis-plus使用Oracle函数生成主键

    函数的调用方式为: select pkg1.fun1 from dual; mybatis-plus一般会使用的主键生成策略为: @Bean public OracleKeyGenerator ora ...

  2. 【Git的基本操作六】分支管理

    分支管理 1. 什么是分支? 在版本控制过程中,使用对条线同时推进多个任务. 2. 分支的好处 同时并行推进多个功能开发,提高开发效率 各个分支在开发过程中,如果某一个分支开发失败,不会对其他分支有任 ...

  3. vue封装swiper

    参考:https://github.com/surmon-china/vue-awesome-swiper npm install vue-awesome-swiper --save 全局引入 imp ...

  4. 2、screen工具

    1.背景 系统管理员经常需要SSH 或者telent 远程登录到Linux 服务器,经常运行一些需要很长时间才能完成的任务,比如系统备份.ftp 传输等等.通常情况下我们都是为每一个这样的任务开一个远 ...

  5. 工控漏洞利用框架 - ISF(Industrial Security Framework)

    一. 框架介绍 本框架主要使用Python语言开发,通过集成ShadowBroker释放的NSA工具Fuzzbunch攻击框架,开发一款适合工控漏洞利用的框架.由于Fuzzbunch攻击框架仅适用于P ...

  6. DNS负载均衡与NGINX负载均衡策略

    负载均衡是指的是把请求均匀的分摊到多个服务器上处理.一般常见的负载均衡有两种:①客户端与反向代理服务器之间的DNS负载均衡②反向代理服务器与应用服务器之间的负载均衡(这种负载均衡有很多,可以是webl ...

  7. [Suricata]无法禁用某些规则的解决办法

    背景: 生产环境中部署了suricata,日常规则更新使用suricata-update,如果想禁用某些规则,可以在配置文件/etc/suricata/disable.conf中添加,比如: #禁用规 ...

  8. Linux下python3的安装以及redis的使用

    python3的安装 上传Python-3.5.2.tar.xz软件到 /server/tools 中 解压 :tar xf   Python-3.5.2.tar.xz 编译安装cd Python-3 ...

  9. 网络协议相关面试问题-https加密算法相关面试问题

    密钥: 密钥是一种参数,它是在使用密码cipher算法过程中输入的参数,同一个明文在相同的密码算法和不同的密钥计算下会产生不同的密文.所以说算法既使公开了但是密钥参数不同其结果也是不同的,其中关于明文 ...

  10. GitChat·人工智能 | 除了深度学习,机器翻译还需要啥?

    本文开始要写作的时候,翻译圈里出了一个“爆炸性”的事件.6月27日下午,一个同传译员在朋友圈里爆料:某AI公司请这位译员去“扮演”机器同传,制造人工智能取代人工同传的“震撼”效果. 这个事件瞬间在译员 ...