re模块

就其本质而言,正则表达式(或 RE)是一种小型的、高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现。正则表达式模式被编译成一系列的字节码,然后由用 C 编写的匹配引擎执行。

字符匹配(普通字符,元字符):

普通字符:大多数字符和字母都会和自身匹配

re.findall('alvin','yuanaleSxalexwupeiqi')['alvin']

元字符: .  ^ $ * + ? { } [ ] | ( ) \

元字符之. ^ $ * + ? { }

import reret=re.findall('a..in','helloalvin')print(ret)#['alvin']

ret=re.findall('^a...n','alvinhelloawwwn')print(ret)#['alvin']

ret=re.findall('a...n$','alvinhelloawwwn')print(ret)#['awwwn']

ret=re.findall('a...n$','alvinhelloawwwn')print(ret)#['awwwn']

ret=re.findall('abc*','abcccc')#贪婪匹配[0,+oo]  print(ret)#['abcccc']

ret=re.findall('abc+','abccc')#[1,+oo]print(ret)#['abccc']

ret=re.findall('abc?','abccc')#[0,1]print(ret)#['abc']

ret=re.findall('abc{1,4}','abccc')print(ret)#['abccc'] 贪婪匹配

注意:前面的*,+,?等都是贪婪匹配,也就是尽可能匹配,后面加?号使其变成惰性匹配

ret=re.findall('abc*?','abcccccc')print(ret)#['ab']

元字符之字符集[]:

#--------------------------------------------字符集[]ret=re.findall('a[bc]d','acd')print(ret)#['acd']

ret=re.findall('[a-z]','acd')print(ret)#['a', 'c', 'd']

ret=re.findall('[.*+]','a.cd+')print(ret)#['.', '+']

#在字符集里有功能的符号: - ^ \

ret=re.findall('[1-9]','45dha3')print(ret)#['4', '5', '3']

ret=re.findall('[^ab]','45bdha3')print(ret)#['4', '5', 'd', 'h', '3']

ret=re.findall('[\d]','45bdha3')print(ret)#['4', '5', '3']

元字符之转义符\

反斜杠后边跟元字符去除特殊功能,比如\.

反斜杠后边跟普通字符实现特殊功能,比如\d

\d  匹配任何十进制数;它相当于类 [0-9]。\D 匹配任何非数字字符;它相当于类 [^0-9]。\s  匹配任何空白字符;它相当于类 [ \t\n\r\f\v]。\S 匹配任何非空白字符;它相当于类 [^ \t\n\r\f\v]。\w 匹配任何字母数字字符;它相当于类 [a-zA-Z0-9_]。\W 匹配任何非字母数字字符;它相当于类 [^a-zA-Z0-9_]\b  匹配一个特殊字符边界,比如空格 ,&,#等

ret=re.findall('I\b','I am LIST')print(ret)#[]ret=re.findall(r'I\b','I am LIST')print(ret)#['I']

现在我们聊一聊\,先看下面两个匹配:

#-----------------------------eg1:import reret=re.findall('c\l','abc\le')print(ret)#[]ret=re.findall('c\\l','abc\le')print(ret)#[]ret=re.findall('c\\\\l','abc\le')print(ret)#['c\\l']ret=re.findall(r'c\\l','abc\le')print(ret)#['c\\l']

#-----------------------------eg2:#之所以选择\b是因为\b在ASCII表中是有意义的m = re.findall('\bblow', 'blow')print(m)m = re.findall(r'\bblow', 'blow')print(m)

元字符之分组()

m = re.findall(r'(ad)+', 'add')print(m)

ret=re.search('(?P<id>\d{2})/(?P<name>\w{3})','23/com')print(ret.group())#23/comprint(ret.group('id'))#23

元字符之|

ret=re.search('(ab)|\d','rabhdg8sd')print(ret.group())#ab

re模块下的常用方法

import re#1re.findall('a','alvin yuan')    #返回所有满足匹配条件的结果,放在列表里#2re.search('a','alvin yuan').group()  #函数会在字符串内查找模式匹配,只到找到第一个匹配然后返回一个包含匹配信息的对象,该对象可以通过调用group()方法得到匹配的字符串,如果字符串没有匹配,则返回None。#3re.match('a','abc').group()     #同search,不过尽在字符串开始处进行匹配#4ret=re.split('[ab]','abcd')     #先按'a'分割得到''和'bcd',在对''和'bcd'分别按'b'分割print(ret)#['', '', 'cd']#5ret=re.sub('\d','abc','alvin5yuan6',1)print(ret)#alvinabcyuan6ret=re.subn('\d','abc','alvin5yuan6')print(ret)#('alvinabcyuanabc', 2) #6obj=re.compile('\d{3}')ret=obj.search('abc123eeee')print(ret.group())#123
import reret=re.finditer('\d','ds3sy4784a')print(ret)        #<callable_iterator object at 0x10195f940>print(next(ret).group())print(next(ret).group())

注意:

import re

ret=re.findall('www.(baidu|oldboy).com','www.oldboy.com')print(ret)#['oldboy']     这是因为findall会优先把匹配结果组里内容返回,如果想要匹配结果,取消权限即可ret=re.findall('www.(?:baidu|oldboy).com','www.oldboy.com')print(ret)#['www.oldboy.com']

补充1:

import reprint(re.findall("<(?P<tag_name>\w+)>\w+</(?P=tag_name)>","<h1>hello</h1>"))print(re.search("<(?P<tag_name>\w+)>\w+</(?P=tag_name)>","<h1>hello</h1>"))print(re.search(r"<(\w+)>\w+</\1>","<h1>hello</h1>"))

补充2:

#匹配出所有的整数import re#ret=re.findall(r"\d+{0}]","1-2*(60+(-40.35/5)-(-4*3))")ret=re.findall(r"-?\d+\.\d*|(-?\d+)","1-2*(60+(-40.35/5)-(-4*3))")ret.remove("")print(ret)

识别图中二维码,领取python全套视频资料

python模块学习(四)的更多相关文章

  1. 【转】Python模块学习 - fnmatch & glob

    [转]Python模块学习 - fnmatch & glob 介绍 fnmatch 和 glob 模块都是用来做字符串匹配文件名的标准库. fnmatch模块 大部分情况下使用字符串匹配查找特 ...

  2. 【目录】Python模块学习系列

    目录:Python模块学习笔记 1.Python模块学习 - Paramiko  - 主机管理 2.Python模块学习 - Fileinput - 读取文件 3.Python模块学习 - Confi ...

  3. Python模块学习filecmp文件比较

    Python模块学习filecmp文件比较 filecmp模块用于比较文件及文件夹的内容,它是一个轻量级的工具,使用非常简单.python标准库还提供了difflib模块用于比较文件的内容.关于dif ...

  4. Python基础学习四

    Python基础学习四 1.内置函数 help()函数:用于查看内置函数的用途. help(abs) isinstance()函数:用于判断变量类型. isinstance(x,(int,float) ...

  5. 解惑Python模块学习,该如何着手操作...

    Python模块 晚上和朋友聊天,说到公司要求精兵计划,全员都要有编程能力.然后C.Java.Python-对于零基础入门的,当然是选择Python的人较多了.可朋友说他只是看了简单的语法,可pyth ...

  6. python模块学习第 0000 题

    将你的 QQ 头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示效果. 类似于图中效果: 好可爱>%<! 题目来源:https://github.com/Yixiao ...

  7. Python模块学习:logging 日志记录

    原文出处: DarkBull    许多应用程序中都会有日志模块,用于记录系统在运行过程中的一些关键信息,以便于对系统的运行状况进行跟踪.在.NET平台中,有非常著名的第三方开源日志组件log4net ...

  8. Python模块学习

    6. Modules If you quit from the Python interpreter and enter it again, the definitions you have made ...

  9. 扩展Python模块系列(四)----引用计数问题的处理

    承接上文,发现在使用Python C/C++ API扩展Python模块时,总要在各种各样的地方考虑到引用计数问题,稍不留神可能会导致扩展的模块存在内存泄漏.引用计数问题是C语言扩展Python模块最 ...

随机推荐

  1. 纪念我人生中第一个merge into语句

    做按组织关系汇总功能时,当数据量特别大,或者汇总组织特别多时,运行效率特别低,于是使用了merge into语句. 代码如下: public void updateInsertData(DataSet ...

  2. PHP经常使用正則表達式汇总

    1.    平时做站点常常要用正則表達式,以下是一些解说和样例,仅供大家參考和改动使用:  2.    "^\d+$" //非负整数(正整数 + 0)  3.    "^ ...

  3. Silverlight实例教程 - Validation数据验证开篇

    Silverlight 4 Validation验证实例系列 Silverlight实例教程 - Validation数据验证开篇 Silverlight实例教程 - Validation数据验证基础 ...

  4. Mybatis(三):MyBatis缓存详解

    MyBatis缓存分为一级缓存和二级缓存 一级缓存 MyBatis的一级缓存指的是在一个Session域内,session为关闭的时候执行的查询会根据SQL为key被缓存(跟mysql缓存一样,修改任 ...

  5. 【翻译自mos文章】在Oracle GoldenGate中循环使用ggserr.log的方法

    在OGG中循环使用ggserr.log的方法: 參考原文: OGG How Do I Recycle The "ggserr.log" File? (Doc ID 967932.1 ...

  6. 23:LVS客户端配置脚本案例

    [root@web03 scripts]# cat prevent_arp.sh #!/bin/bash lo_ip=$(ip a s lo|grep "10.0.0.1[3]/32&quo ...

  7. 依赖Spring的情况下,Java Web项目如何在启动时加载数据库中的数据?

    原文:https://blog.csdn.net/u012345283/article/details/39558537 原文:https://blog.csdn.net/wandrong/artic ...

  8. Linux下mongodb安装及数据导入导出教程

    Linux下mongodb安装及数据导入导出教程 #查看linux发行版本 cat /etc/issue #查看linux内核版本号 uname -r 一.Linux下mongodb安装的一般步骤 1 ...

  9. ssh 面试

    Struts1工作原理1. 初始化:struts框架的总控制器ActionServlet是一个Servlet,它在web.xml中配置成自动启动的Servlet,在启动时总 控制器会读取配置文件(st ...

  10. 扩展mysql - 手把手教你写udf

    1 MySQL简介 MySQL是最流行的开放源码SQL数据库管理系统,相对于Oracle,DB2等大型数据库系统,MySQL由于其开源性.易用性.稳定性等特点,受到个人使用者.中小型企业甚至一些大型企 ...