[Python Study Notes]正则表达式

正则表达式

正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配。

Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式。

re 模块使 Python 语言拥有全部的正则表达式功能。

compile 函数根据一个模式字符串和可选的标志参数生成一个正则表达式对象。该对象拥有一系列方法用于正则表达式匹配和替换。

re 模块也提供了与这些方法功能完全一致的函数,这些函数使用一个模式字符串做为它们的第一个参数。

本章节主要介绍Python中常用的正则表达式处理函数。


re.match函数

re.match 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回none。

函数语法

re.match(pattern, string, flags=0)
参数 描述
pattern 匹配的正则表达式
string 要匹配的字符串。
flags 标志位,用于控制正则表达式的匹配方式,如:是否区分大小写,多行匹配等等。

匹配成功re.match方法返回一个匹配的对象,否则返回None。

我们可以使用group(num) 或 groups() 匹配对象函数来获取匹配表达式。

匹配对象方法 描述
group(num=0) 匹配的整个表达式的字符串,group() 可以一次输入多个组号,在这种情况下它将返回一个包含那些组所对应值的元组。
groups() 返回一个包含所有小组字符串的元组,从 1 到 所含的小组号。
1 # -*- coding: UTF-8 -*-
2
3 import re
4 print(re.match(‘www‘, ‘www.runoob.com‘).span())  # 在起始位置匹配
5 print(re.match(‘com‘, ‘www.runoob.com‘))         # 不在起始位置匹配

结果:

1 (0, 3)
2 None
 1 import re
 2
 3 line = "Cats are smarter than dogs"
 4
 5 matchObj = re.match( r‘(.*) are (.*?) .*‘, line, re.M|re.I)
 6
 7 if matchObj:
 8    print "matchObj.group() : ", matchObj.group()
 9    print "matchObj.group(1) : ", matchObj.group(1)
10    print "matchObj.group(2) : ", matchObj.group(2)
11 else:
12    print "No match!!"

结果 :

1 matchObj.group() :  Cats are smarter than dogs
2 matchObj.group(1) :  Cats
3 matchObj.group(2) :  smarter

原文地址:https://www.cnblogs.com/liu66blog/p/8257006.html

时间: 2024-08-26 16:57:18

[Python Study Notes]正则表达式的相关文章

[Python Study Notes]计算cpu使用率

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: cpu使用率.py >>作者: liu yang >>邮箱: [email protected] ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

[Python Study Notes]计算cpu使用率v0.1

V0.1 更新日志: 1.加入平台判断,支持windows与linux 2.自动清屏显示,显示更加直观 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: cpu使用率.py >>作者: liu yang >>邮箱: [email protected] ''''''''''''''''''''''''''''''

[Python Study Notes]磁盘分区和io性能

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: 磁盘信息.py >>作者: liu yang >>邮箱: [email protected] ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

[Python Study Notes]内存信息

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: 内存信息.py >>作者: liu yang >>邮箱: [email protected] ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

[Python Study Notes]电池信息

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: 电池信息.py >>作者: liu yang >>邮箱: [email protected] ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

[Python Study Notes]cpu信息

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: cpu信息.py >>作者: liu yang >>邮箱: [email protected] '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

[Python Study Notes]进程信息(丁丁软件监控进程,http-post)

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: 进程信息.py >>作者: liu yang >>邮箱: [email protected] ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

[Python Study Notes]CS架构远程访问获取信息--Client端

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: ps_client.py >>作者: liu yang >>邮箱: [email protected] '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

[Python Study Notes]CS架构远程访问获取信息--SERVER端

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: ps_server.py >>作者: liu yang >>邮箱: [email protected] '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''