1. 匹配单个字符和数字

. --->> 匹配除换行符以外的任意字符。
[0123456789] --->> []字符集合,表示匹配方括号中所包含的任意一个字符。
[Thomas] --->> []字符集合,表示匹配方括号中所包含的任意一个字符。匹配'T','h'...任意一个字符。
[a-z] --->> - 匹配任意小写字母
[A-Z] --->> - 匹配任意大写字母
[0-9] --->> 匹配任意数字,类似于[0123456789]
[0-9a-zA-Z] --->> 匹配任意的数字和字母(包含大小写)
[0-9a-zA-Z_] --->> 匹配任意的数字和字母(包含大小写)和下划线
[^Thomas] --->> 匹配除了Thomas这几个字母以外的所有字符,中括号里面的^称为脱字符,表示不匹配集合中的字符。
[^0-9] --->> 匹配所有的非数字字符。
\d --->> 匹配数字,效果同[0-9]
\D --->> 匹配非数字字符,效果同[^0-9]
\w --->> 匹配数字,字母和下划线,效果同[0-9a-zA-Z_],判断标识符比较合适
\W --->> 匹配数字,字母和下划线,效果同[^0-9a-zA-Z_],判断标识符比较合适
\s --->> 匹配任意的空白符(空格,换行,回车,换页,制表),效果同[ \f\n\r\t]
\S --->> 匹配任意的非空白符(空格,换行,回车,换页,制表),效果同[^ \f\n\r\t]

print(re.search(".","Thomas is a good man"))
# <re.Match object; span=(, ), match='T'> print(re.search("[0123456789]","Thomas is a good man_ 7"))
# <re.Match object; span=(, ), match=''> print(re.search("[Thomas]","Thomas is a good man_ 7"))
# <re.Match object; span=(, ), match='T'> print(re.search("[a-z]","Thomas is a good man_ 7"))
# <re.Match object; span=(, ), match='h'> print(re.search("[A-Z]","Thomas is a good man_ 7"))
# <re.Match object; span=(, ), match='T'> print(re.search("[0-9]","Thomas is a good man_ 7"))
# <re.Match object; span=(, ), match=''> print(re.search("[0-9a-zA-Z]","Thomas is a good man_ 7"))
# <re.Match object; span=(, ), match='T'> print(re.search("[0-9a-zA-Z_]","_Thomas is a good man_ 7"))
# <re.Match object; span=(, ), match='_'> print(re.search("[^Thomas]","_Thomas is a good man_ 7"))
# <re.Match object; span=(, ), match='_'> print(re.search("[^0-9]","Thomas is a good man_ 7"))
# <re.Match object; span=(, ), match='T'> print(re.search("\d","Thomas is a good man 7"))
# <re.Match object; span=(, ), match=''> print(re.search("\w","_Thomas is a good man 7"))
# <re.Match object; span=(, ), match='_'> print(re.search("\s","_Thomas is a good man 7"))
# <re.Match object; span=(, ), match=' '>

2. 锚字符(边界字符)

^ --->> 行首匹配,和在[]里的^不是一个意思
$ --->> 行尾匹配
\A --->> 匹配字符串开始,它和^区别是:\Z只匹配整个字符的开头,即是re.M模式下也不会匹配它行的行首。
\Z --->> 匹配字符串结束,它和$区别是:\Z只匹配整个字符的开头,即是re.M模式下也不会匹配它行的行尾。
\b --->> 匹配单词的边界,也就是值单词和空格间的位置。r'er\b':可以匹配never,不能匹配nerve,单词的边界。
\B --->> 匹配非单词的边界,也就是值单词和空格间的位置。

print(re.search("^Thomas","Thomas is a good man 7"))
# <re.Match object; span=(, ), match='Thomas'> print(re.search("Thomas$","Thomas is a good man Thomas"))
# <re.Match object; span=(, ), match='Thomas'> print(re.findall("^Thomas","Thomas is a good man\nThomas is a nice man",re.M))
# ['Thomas', 'Thomas'] print(re.findall("\AThomas","Thomas is a good man\nThomas is a nice man",re.M))
# ['Thomas'] print(re.findall("man$","Thomas is a good man\nThomas is a nice man",re.M))
# ['man', 'man'] print(re.findall("man\Z","Thomas is a good man\nThomas is a nice man",re.M))
# ['man'] print(re.search(r"er\b","never"))
# <re.Match object; span=(, ), match='er'>
print(re.search(r"er\b","nerve"))
# None print(re.search(r"er\B","never"))
# None
print(re.search(r"er\B","nerve"))
# <re.Match object; span=(, ), match='er'>

3. 匹配多个字符

说明:下方的x、y、z均为假设的普通字符,n、m是非负整数,不是正则表达式的元字符。
(xyz) --->> 匹配小括号内的xyz(作为一个整体去匹配)
x? --->> 匹配0个或者1个x。
x* --->> 匹配0个或者任意多个个x。(.* 表示匹配0个或者任意多个字符(换行符除外))
x+ --->> 匹配至少一个x
x{n} --->> 匹配确定n个x(n是一个非负整数)
x{n,} --->> 匹配至少n个x
x{n,m} --->> 匹配至少n个最多m个x,注意n<=m。
x|y --->> | 表示或,匹配的是x或y

print(re.findall(r"(Thomas)","Thomasgood is a good man,Thomas is a nice man"))
# ['Thomas', 'Thomas'] print(re.findall(r"o?","ooo")) # 非贪婪匹配(尽可能少的匹配)
# ['o', 'o', 'o', ''] print(re.findall(r"o*","oooboo")) # 贪婪匹配(尽可能多的匹配)
# ['ooo', '', 'oo', ''] print(re.findall(r".*","oooboo"))
# ['oooboo', ''] print(re.findall(r"o+","oooboo"))
# ['ooo', 'oo'] print(re.findall(r"o{2}","oooboo"))
# ['oo', 'oo'] print(re.findall(r"o{2,}","oooboo")) # 也是贪婪匹配
# ['ooo', 'oo'] print(re.findall(r"a{3,6}","aaaabaaa"))
# ['aaaa', 'aaa'] # 最多6个也可以是4个 print(re.findall(r"((t|T)homas)","thomas--Thomas"))
# [('thomas', 't'), ('Thomas', 'T')] # 这是组 print(re.findall(r"(t|T)homas","thomas--Thomas"))
# ['t', 'T']
# 需求,提取Thomas****man
str = "Thomas is a good man!Thomas is a nice man!Thomas is a very handsome man"
print(re.findall(r"Thomas.*?man",str))
# ['Thomas is a good man', 'Thomas is a nice man', 'Thomas is a very handsome man']

4. 特殊形式

*? +? ?? --->> 最小匹配,通常都是尽可能多的匹配,可以使用这种方式解决贪婪匹配。
(?:x) --->> 类似(xyz),单不表示一个组

# 注释:/* part1  */ /* part2 */
print(re.findall(r"//*.*?/*/",r"/* part1 */ /* part2 */"))
# ['/* part1 */', '/* part2 */']

5. 实例

  我们在回过头来解决那个电话号码的问题。

def checkPhone2(str):
#
pat = r"^1(([34578]\d)|(47))\d{8}$"
res = re.match(pat,str)
return res print(checkPhone2(""))
# <re.Match object; span=(, ), match=''>
print(checkPhone2(""))
# None
print(checkPhone2("1391234a678"))
# None
print(checkPhone2(""))
# None
print(checkPhone2(""))
# None

Python笔记_第四篇_高阶编程_正则表达式_2.正则表达式入门的更多相关文章

  1. Python笔记_第四篇_高阶编程_进程、线程、协程_5.GPU加速

    Numba:高性能计算的高生产率 在这篇文章中,笔者将向你介绍一个来自Anaconda的Python编译器Numba,它可以在CUDA-capable GPU或多核cpu上编译Python代码.Pyt ...

  2. Python开发【第十三篇】高阶函数、递归函数、闭包

    函数式编程是指用一系列函数解决问题 好处:用每个函数完成每个细小的功能,一系列函数任意组合能够解决大问题 函数仅仅接收输入并产生输出,不包含任何能影响输出的内部状态 函数之间的可重入性 当一个函数的输 ...

  3. python学习三十四天函数高阶函数定义及用法

    python函数高阶函数是把函数当成一个变量,传递给函数作为参数,或者函数的返回值里面有函数,都称为高阶函数, 1,把函数作为参数传递 def dac(x,y): return x+y def tes ...

  4. Python笔记_第四篇_高阶编程_实例化方法、静态方法、类方法和属性方法概念的解析。

    1.先叙述静态方法: 我们知道Python调用类的方法的时候都要进行一个实例化的处理.在面向对象中,一把存在静态类,静态方法,动态类.动态方法等乱七八糟的这么一些叫法.其实这些东西看起来抽象,但是很好 ...

  5. Python笔记_第四篇_高阶编程_魔法(术)方法详解(重载的再详解)

    1. 魔法方法是什么? 魔法方法(Magic Method)是Python比较独特的应用,它可以给你的类增加特殊的方法,如果你的对象实现了(重载),这些方法中的某一个,就会被Python所调用.正如装 ...

  6. Python笔记_第四篇_高阶编程_再议装饰器和再议内置函数

    1. 概述: 我们在前面用了很多的装饰器这个工具的方法.这个位置要系统的讲一下装饰器. 1.2 为什么需要装饰器. 装饰器本质是一个Python函数,它可以让其他函数在不需要任何代码变动的前提下增加额 ...

  7. Python笔记_第四篇_高阶编程_进程、线程、协程_4.协程

    1.协程的概念: 子程序或者子函数,在所有语言中都是层级调用,比如A调用B,再B执行的过程中又可以调用C,C执行完毕返回,B执行返回,最后是A执行完毕返回.是通过栈来实现的,一个线程就是执行一个自称, ...

  8. Python笔记_第四篇_高阶编程_进程、线程、协程_3.进程vs线程

    1.多任务的实现原理: 通常我们会设计Mater-Workder模式,Master负责分配任务,Worker负责执行任务,因此多任务环境下,通常是一个Master,多个Worker 2.多进程: 主进 ...

  9. Python笔记_第四篇_高阶编程_进程、线程、协程_1.进程

    1. 多任务原理: 现代操作系统,像win,max os x,linux,unix等都支持多任务. * 什么叫做多任务? 操作系统可以同时运行多个任务. * 单核CPU实现多任务原理? 操作系统轮流让 ...

随机推荐

  1. 用fiddler监控移动端的通讯

    用fiddler监控移动端的通讯  1 依次打开Fiddler->Tools->Fiddler Options在[Connection]面板里将Allow remote computers ...

  2. python 首先生成包含1000个随机字符的字符串,然后统计每个字符的出现次数

    题目:首先生成包含1000个随机字符的字符串,然后统计每个字符的出现次数 import string import random x = string.ascii_letters + string.d ...

  3. tomcat-jvm内存问题

    http://www.360doc.com/content/14/0617/12/114824_387440563.shtml http://27091497.blog.163.com/blog/st ...

  4. http请求的过程及潜在的性能优化点

    web前端的看富于部署过程 开发者将开发的代码发布到远程的服务器(webserver/cdn),用户通过访问浏览器输入相应的网址,浏览器向远程服务器发送请求,动态的增量式的加载资源 web前端就是一个 ...

  5. 17.swoole学习笔记--异步mysql操作

    <?php //异步mysql操作 $db=new swoole_mysql(); $config=[ 'host'=>'192.168.10.31', 'user'=>'zouke ...

  6. 第一个flink application

    导入maven依赖 需要注意的是,如果使用scala写程序,导入的依赖跟java是不一样的 Maven Dependencies You can add the following dependenc ...

  7. js利用递归生成随机数填充到数组

    用递归算法实现,数组长度为5且元素的随机数在2-32间不重复的值 var  array = new Array(5); function addNumToArray(array,num){     i ...

  8. Delphi MD5

    unit uMD5; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics; type MD5Count = ...

  9. 15. react UI组件和容器组件的拆分 及 无状态组件

    1.组件的拆分 组件拆分的前提 当所有的逻辑都出现在一个组件内时 组件会变得非常复杂 不便与代码的维护 所以对组件进行拆分 IU组件 进行页面渲染 容器组件  进行逻辑操作 UI组件的拆分 新建一个 ...

  10. java课程之团队开发冲刺阶段2.3

    一.总结昨天进度 1.完成整合功能 二.遇到的问题 1.在整合的过程中,总是发现在switch开关提醒了两次,后来发现是因为我使用了setChecked方法,而这个方法触发onCheckedChang ...