re模块(* * * * *)

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

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

1 普通字符:大多数字符和字母都会和自身匹配
              >>> re.findall('alvin','yuanaleSxalexwupeiqi')
                      ['alvin']

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

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

import re

ret=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 re
ret=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/com
print(ret.group('id'))#

元字符之|

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

re模块下的常用方法

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

re 正则模块的更多相关文章

  1. Python全栈开发【re正则模块】

    re正则模块 本节内容: 正则介绍 元字符及元字符集 元字符转义符 re模块下的常用方法 正则介绍(re) 正则表达式(或 RE)是一种小型的.高度专业化的编程语言. 在Python中,它内嵌在Pyt ...

  2. python_way day6 反射,正则 模块(进度条,hash)

    python_way day6 反射 正则 模块 sys,os,hashlib 一.模块: 1.sys & os: 我们在写项目的时候,经常遇到模块互相调用的情况,但是在不同的模块下我们通过什 ...

  3. 小白的Python之路 day5 re正则模块

    re正则模块 一.概述 就其本质而言,正则表达式(或 RE)是一种小型的.高度专业化的编程语言,要讲他的具体用法要讲一本书!它内嵌在Python中,并通过 re 模块实现.你可以为想要匹配的相应字符串 ...

  4. 认识python正则模块re

    python正则模块re python中re中内置匹配.搜索.替换方法见博客---python附录-re.py模块源码(含re官方文档链接) 正则的应用是处理一些字符串,phthon的博文python ...

  5. Python3中正则模块re.compile、re.match及re.search函数用法详解

    Python3中正则模块re.compile.re.match及re.search函数用法 re模块 re.compile.re.match. re.search 正则匹配的时候,第一个字符是 r,表 ...

  6. 008---re正则模块

    re正则模块 字符串的匹配规则 匹配模式 re.match() re.search() re.findall() re.split() re.sub() 元字符 print('------------ ...

  7. day22、模块-basedir、os、json模块、pickle和正则模块。

    四.正则. re模块: 作用:针对的对象:字符串, 课前引入: 例子一. s='dsdsadsadadsalexdsds's.find('alex') 如何找到字符串内部的alex;?过去学习可使用方 ...

  8. 21 re正则模块 垃圾回收机制

    垃圾回收机制 不能被程序访问到的数据,就称之为垃圾 引用计数 引用计数:用来记录值的内存地址被记录的次数的:当一个值的引用计数为0时,该值就会被系统的垃圾回收机制回收 每一次对值地址的引用都可以使该值 ...

  9. Python——正则模块

    1.re模块是用来操作正则表达式 2.正则表达式——用来字符串匹配的 (1)字符组:[字符组]  例如[0123fdsa456*/-] [0-9] 等同于[0123456789] [a-z] 匹配小写 ...

  10. Go语言正则模块

    基本使用 import "bytes" import "fmt" import "regexp" func main() { //这个测试一 ...

随机推荐

  1. Codeforces 983B. XOR-pyramid【区间DP】

    LINK 定义了一种函数f 对于一个数组b 当长度是1的时候是本身 否则是用一个新的数组(长度是原数组-1)来记录相邻数的异或,对这个数组求函数f 大概是这样的: \(f(b[1]⊕b[2],b[2] ...

  2. json-patch 了解

    What is JSON Patch? JSON Patch is a format for describing changes to a JSON document. It can be used ...

  3. Python编程核心内容 ---- Function(函数)

    Python版本:3.6.2  操作系统:Windows  作者:SmallWZQ 截至上篇随笔<Python数据结构之四——set(集合)>,Python基础知识也介绍好了.接下来准备干 ...

  4. CCNode的属性说明

    class CC_DLL CCNode : public CCObject { protected://属性列表 float m_fRotationX; ///x轴旋转角度 float m_fRota ...

  5. nginx rewrite规则实例讲解

    一.正则表达式匹配,其中: * ~ 为区分大小写匹配* ~* 为不区分大小写匹配* !~和!~*分别为区分大小写不匹配及不区分大小写不匹配 二.文件及目录匹配,其中:* -f和!-f用来判断是否存在文 ...

  6. emacs之配置etags

    emacsConfig/etags-setting.el (require 'auto-complete-etags) (setq ac-sources (append '(ac-source-eta ...

  7. windows 内存分配回收检查工具

    LeakDiag是微软一款检测memory leak的工具,使用比较简单 首先去下载一个ftp://ftp.microsoft.com/PSS/Tools/Developer%20Support%20 ...

  8. Java 运用流传输文件

    实例1 package IO; import java.io.FileReader; import java.io.FileWriter; import java.io.Reader; import ...

  9. Spring MVC的困惑url-pattern /和/*的区别

    今天在写项目时发现一个spring 总是报org.springframework.web.servlet.DispatcherServlet noHandlerFound警告: No mapping ...

  10. Getting Started(入门)

    欢迎阅读专门针对android开发者的培训课程,在这一系列的课程中,描述了如何通过我们的示例代码来完成特定的任务和功能,这些代码可以灵活地应用到你的应用程序中. 课程被分成了几部分, 第一部分,入门, ...