一、正则表达式的特殊字符介绍

正则表达式
^ 匹配行首
$ 匹配行尾
. 任意单个字符
[] 匹配包含在中括号中的任意字符
[^] 匹配包含在中括号中的字符之外的字符
[-] 匹配指定范围的任意单个字符
? 匹配之前项的1次或者0次
+ 匹配之前项的1次或者多次
* 匹配之前项的0次或者多次
{n} 匹配之前项的n次
{m,n} 匹配之前项最大n次,最小m次
{n,} 配置之前项至少n次

  

二、re模块的方法介绍

1、匹配类方法

a、findall方法
# findall方法,该方法在字符串中查找模式匹配,将所有的匹配字符串以列表的形式返回,如果文本中没有任何字符串匹配模式,则返回一个空的列表,
# 如果有一个子字符串匹配模式,则返回包含一个元素的列表,所以,无论怎么匹配,我们都可以直接遍历findall返回的结果而不会出错,这对工程师
# 编写程序来说,减少了异常情况的处理,代码逻辑更加简洁

  

# re.findall() 用来输出所有符合模式匹配的子串

re_str = "hello this is python 2.7.13 and python 3.4.5"

pattern = "python [0-9]\.[0-9]\.[0-9]"
res = re.findall(pattern=pattern,string=re_str)
print(res) # ['python 2.7.1', 'python 3.4.5'] pattern = "python [0-9]\.[0-9]\.[0-9]{2,}"
res = re.findall(pattern=pattern,string=re_str)
print(res) # ['python 2.7.13'] pattern = "python[0-9]\.[0-9]\.[0-9]{2,}"
res = re.findall(pattern=pattern,string=re_str)
print(res) # [] # re.findall() 方法,返回一个列表,如果匹配到的话,列表中的元素为匹配到的子字符串,如果没有匹配到,则返回一个空的列表 re_str = "hello this is python 2.7.13 and Python 3.4.5" pattern = "python [0-9]\.[0-9]\.[0-9]"
res = re.findall(pattern=pattern,string=re_str,flags=re.IGNORECASE)
print(res) # ['python 2.7.1', 'Python 3.4.5'] # 设置标志flags=re.IGNORECASE,意思为忽略大小写

  

b、编译的方式使用正则表达式
# 我们一般采用编译的方式使用python的正则模块,如果在大量的数据量中,编译的方式使用正则性能会提高很多,具体读者们可以可以实际测试
re_str = "hello this is python 2.7.13 and Python 3.4.5"
re_obj = re.compile(pattern = "python [0-9]\.[0-9]\.[0-9]",flags=re.IGNORECASE)
res = re_obj.findall(re_str)
print(res)

  

c、match方法
# match方法,类似于字符串中的startwith方法,只是match应用在正则表达式中更加强大,更富有表现力,match函数用以匹配字符串的开始部分,如果模式
# 匹配成功,返回一个SRE_Match类型的对象,如果模式匹配失败,则返回一个None,因此对于普通的前缀匹配,他的用法几乎和startwith一模一样,例如我
# 们要判断data字符串是否以what和是否以数字开头

  

s_true = "what is a boy"
s_false = "What is a boy"
re_obj = re.compile("what") print(re_obj.match(string=s_true))
# <_sre.SRE_Match object; span=(0, 4), match='what' print(re_obj.match(string=s_false))
# None s_true = "123what is a boy"
s_false = "what is a boy" re_obj = re.compile("\d+") print(re_obj.match(s_true))
# <_sre.SRE_Match object; span=(0, 3), match='123'> print(re_obj.match(s_true).start())
# 0
print(re_obj.match(s_true).end())
# 3
print(re_obj.match(s_true).string)
# 123what is a boy
print(re_obj.match(s_true).group())
# 123 print(re_obj.match(s_false))
# None

  

d、search方法
# search方法,模式匹配成功后,也会返回一个SRE_Match对象,search方法和match的方法区别在于match只能从头开始匹配,而search可以从
# 字符串的任意位置开始匹配,他们的共同点是,如果匹配成功,返回一个SRE_Match对象,如果匹配失败,返回一个None,这里还要注意,
# search仅仅查找第一次匹配,也就是说一个字符串中包含多个模式的匹配,也只会返回第一个匹配的结果,如果要返回所有的结果,最简单
# 的方法就是findall方法,也可以使用finditer方法

  

e、finditer方法
# finditer返回一个迭代器,遍历迭代器可以得到一个SRE_Match对象,比如下面的例子

  

re_str = "what is a different between python 2.7.14 and python 3.5.4"

re_obj = re.compile("\d{1,}\.\d{1,}\.\d{1,}")

for i in re_obj.finditer(re_str):
print(i) # <_sre.SRE_Match object; span=(35, 41), match='2.7.14'>
# <_sre.SRE_Match object; span=(53, 58), match='3.5.4'>

  

2、修改类方法介绍

a、sub方法
# re模块sub方法类似于字符串中的replace方法,只是sub方法支持使用正则表达式,所以,re模块的sub方法使用场景更加广泛

  

re_str = "what is a different between python 2.7.14 and python 3.5.4"

re_obj = re.compile("\d{1,}\.\d{1,}\.\d{1,}")

print(re_obj.sub("a.b.c",re_str,count=1))
# what is a different between python a.b.c and python 3.5.4 print(re_obj.sub("a.b.c",re_str,count=2))
# what is a different between python a.b.c and python a.b.c print(re_obj.sub("a.b.c",re_str))
# what is a different between python a.b.c and python a.b.c

  

b、split方法
# re模块的split方法和python字符串中的split方法功能是一样的,都是将一个字符串拆分成子字符串的列表,区别在于re模块的split方法能够
# 使用正则表达式
# 比如下面的例子,使用. 空格 : !分割字符串,返回的是一个列表

  

re_str = "what is a different between python 2.7.14 and python 3.5.4 USA:NewYork!Zidan.FRA"

re_obj = re.compile("[. :!]")

print(re_obj.split(re_str))
# ['what', 'is', 'a', 'different', 'between', 'python', '2', '7', '14', 'and', 'python', '3', '5', '4', 'USA', 'NewYork', 'Zidan', 'FRA']

  

c、大小写不敏感设置
# 3、大小写不敏感

# re.compile(flags=re.IGNORECASE)

  

d、非贪婪匹配
# 4、非贪婪匹配,贪婪匹配总是匹配到最长的那个字符串,相应的,非贪婪匹配是匹配到最小的那个字符串,只需要在匹配字符串的时候加一个?即可

# 下面的例子,注意两个.
s = "Beautiful is better than ugly.Explicit is better than impliciy." re_obj = re.compile("Beautiful.*y\.") print(re_obj.findall(s))
# ['Beautiful is better than ugly.Explicit is better than implicit.'] re_obj = re.compile("Beautiful.*?\.") print(re_obj.findall(s))
# ['Beautiful is better than ugly.']

  

e、在正则匹配字符串中加一个小括号,会有什么的效果呢?

如果是要配置一个真正的小括号,那么就需要转义符,下面的例子大家仔细看下,注意下search方法返回的对象的group(1)这个方法是报错的

import re
s = "=aa1239d&&& 0a ()--" # obj = re.compile("\(\)")
# search
# rep = obj.search(s)
# print(rep)
# <_sre.SRE_Match object; span=(15, 17), match='()'>
# print(rep.group(1))
# IndexError: no such group
# print(rep.group())
# ()
# findall

rep = obj.findall(s)
print(rep)
# ['()']

如果是要返回括号中匹配的字符串中,则该小括号不需要转义符,findall方法返回的是小伙好中匹配到的字符串,search.group()方法的返回的整个模式匹配到字符串,search.group(1)这个是匹配第一个小括号中的模式匹配到的字符串,search.group(2)这个是匹配第二个小括号中的模式匹配到的字符串,以此类推

s = "=aa1239d&&& 0a ()--"
rep = re.compile("\w+(&+)") print(rep.findall(s))
# ['&&&']
print(rep.search(s).group())
# aa1239d&&&
print(rep.search(s).group(1))
# &&&

python的re模块详解的更多相关文章

  1. python之OS模块详解

    python之OS模块详解 ^_^,步入第二个模块世界----->OS 常见函数列表 os.sep:取代操作系统特定的路径分隔符 os.name:指示你正在使用的工作平台.比如对于Windows ...

  2. python之sys模块详解

    python之sys模块详解 sys模块功能多,我们这里介绍一些比较实用的功能,相信你会喜欢的,和我一起走进python的模块吧! sys模块的常见函数列表 sys.argv: 实现从程序外部向程序传 ...

  3. python中threading模块详解(一)

    python中threading模块详解(一) 来源 http://blog.chinaunix.net/uid-27571599-id-3484048.html threading提供了一个比thr ...

  4. Python中time模块详解

    Python中time模块详解 在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括:time,datetime以及calendar.这篇文章,主要讲解time模块. ...

  5. Python的logging模块详解

          Python的logging模块详解 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.日志级别 日志级别指的是产生的日志的事件的严重程度. 设置一个级别后,严重程度 ...

  6. python中常用模块详解二

    log模块的讲解 Python 使用logging模块记录日志涉及四个主要类,使用官方文档中的概括最为合适: logger提供了应用程序可以直接使用的接口API: handler将(logger创建的 ...

  7. Python中time模块详解(转)

    在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括:time,datetime以及calendar.这篇文章,主要讲解time模块. 在开始之前,首先要说明这几点: ...

  8. python中socket模块详解

    socket模块简介 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket.socket通常被叫做"套接字",用于描述IP地址和端口,是一个通信 ...

  9. python之shutil模块详解

    shutil模块 -- --High-level file operations  高级的文件操作模块. os模块提供了对目录或者文件的新建/删除/查看文件属性,还提供了对文件以及目录的路径操作.比如 ...

随机推荐

  1. Spark RDD 操作

    1. Spark RDD 创建操作 1.1 数据集合   parallelize 可以创建一个能够并行操作的RDD.其函数定义如下: ) scala> sc.defaultParallelism ...

  2. C#实现json压缩和格式化

    json作为常用数据文件,为了传输的效率,在传输前要进行压缩,而在传输后要进行格式化,以便阅读.下面是使用C#完成的格式化和压缩代码. public static string Compress(st ...

  3. JavaScript数组方法--flat、forEach、map

    今天到flat了,一个第一次知道该方法还是看到一个面试题,别人给了个答案,用到了flat才知道的方法. 前面也写过关于这道面试题的文章,<一道关于数组的前端面试题>. 这里再来说说吧! f ...

  4. C#;DataTable添加列;DataTable转List泛型集合;List泛型集合转DataTable泛型集合;

    给DataTable添加列 string sql = "select * from cgpmb order by code"; DataTable dt = Bobole.Data ...

  5. java实现字符串和LIST,MAP转换

    需要下载第三方的jar :net.sf.json import java.io.BufferedReader; import java.io.InputStream; import java.io.I ...

  6. oo第一次总结博客

    一. 多项式求导问题描述 基本概念的声明: 带符号整数 支持前导 0 的带符号整数,符号可忽略,如:+02.-16.19260817 等. 因子 变量因子 幂函数 一般形式 由自变量x和指数组成,指数 ...

  7. Linux下查看tomcat版本

    进入到tomcat的bin目录下,再执行./version.sh tomcat版本:7.0

  8. POJ - 3278

    题目链接:http://poj.org/problem?id=3278 ac代码: #include <iostream>#include <stdio.h>#include ...

  9. js 序列化

    Python 序列化 字符串 = json.dumps(对象)  对象转字符串 对象 = json.loads(字符串)   字符串转对象 Javascript 字符串 = JSON.stringif ...

  10. Eclipse使用Maven创建Web时错误:Could not resolve archetype

    请检查maven的setting 是否有问题.window->Perfenence->maven->User Settings里 看 Gloal Setting和User Setti ...