版权所有,未经许可,禁止转载
<div class="article-child "><h3>章节</h3><ul><li class="page_item page-item-2744"><a href="https://www.qikegu.com/docs/2744">Python 介绍</a></li> <li class="page_item page-item-2750"><a href="https://www.qikegu.com/docs/2750">Python 开发环境搭建</a></li> <li class="page_item page-item-2752"><a href="https://www.qikegu.com/docs/2752">Python 语法</a></li> <li class="page_item page-item-2754"><a href="https://www.qikegu.com/docs/2754">Python 变量</a></li> <li class="page_item page-item-2757"><a href="https://www.qikegu.com/docs/2757">Python 数值类型</a></li> <li class="page_item page-item-2759"><a href="https://www.qikegu.com/docs/2759">Python 类型转换</a></li> <li class="page_item page-item-2761"><a href="https://www.qikegu.com/docs/2761">Python 字符串(String)</a></li> <li class="page_item page-item-2763"><a href="https://www.qikegu.com/docs/2763">Python 运算符</a></li> <li class="page_item page-item-2766"><a href="https://www.qikegu.com/docs/2766">Python 列表(list)</a></li> <li class="page_item page-item-2769"><a href="https://www.qikegu.com/docs/2769">Python 元组(Tuple)</a></li> <li class="page_item page-item-2774"><a href="https://www.qikegu.com/docs/2774">Python 集合(Set)</a></li> <li class="page_item page-item-2776"><a href="https://www.qikegu.com/docs/2776">Python 字典(Dictionary)</a></li> <li class="page_item page-item-2779"><a href="https://www.qikegu.com/docs/2779">Python If … Else</a></li> <li class="page_item page-item-2781"><a href="https://www.qikegu.com/docs/2781">Python While 循环</a></li> <li class="page_item page-item-2783"><a href="https://www.qikegu.com/docs/2783">Python For 循环</a></li> <li class="page_item page-item-2785"><a href="https://www.qikegu.com/docs/2785">Python 函数</a></li> <li class="page_item page-item-2787"><a href="https://www.qikegu.com/docs/2787">Python Lambda</a></li> <li class="page_item page-item-2790"><a href="https://www.qikegu.com/docs/2790">Python 类与对象</a></li> <li class="page_item page-item-2793"><a href="https://www.qikegu.com/docs/2793">Python 继承</a></li> <li class="page_item page-item-2801"><a href="https://www.qikegu.com/docs/2801">Python 迭代器(Iterator)</a></li> <li class="page_item page-item-2803"><a href="https://www.qikegu.com/docs/2803">Python 模块</a></li> <li class="page_item page-item-2806"><a href="https://www.qikegu.com/docs/2806">Python 日期(Datetime)</a></li> <li class="page_item page-item-2808"><a href="https://www.qikegu.com/docs/2808">Python JSON</a></li> <li class="page_item page-item-2810"><a href="https://www.qikegu.com/docs/2810">Python 正则表达式(RegEx)</a></li> <li class="page_item page-item-2813"><a href="https://www.qikegu.com/docs/2813">Python PIP包管理器</a></li> <li class="page_item page-item-2815"><a href="https://www.qikegu.com/docs/2815">Python 异常处理(Try…Except)</a></li> <li class="page_item page-item-2817"><a href="https://www.qikegu.com/docs/2817">Python 打开文件(File Open)</a></li> <li class="page_item page-item-2819"><a href="https://www.qikegu.com/docs/2819">Python 读文件</a></li> <li class="page_item page-item-2821"><a href="https://www.qikegu.com/docs/2821">Python 写文件</a></li> <li class="page_item page-item-2823"><a href="https://www.qikegu.com/docs/2823">Python 删除文件与文件夹</a></li> </ul></div>
正则表达式是组成搜索模式的字符序列。
正则表达式用于按指定的搜索模式搜索字符串。
正则表达式(RegEx)模块
Python有一个名为re
的内置包,用来处理正则表达式。
示例
导入re
模块:
import re
Python中的正则表达式
导入re
模块后,就可以开始使用正则表达式:
示例
搜索字符串,查看是否以“the”开头,以“Spain”结尾:
import re
txt = "The rain in Spain"
x = re.search("^The.*Spain$", txt)
正则表达式函数
re
模块提供了一组函数,用于搜索匹配的字符串:
<table> <tbody><tr> <th style="width:20%;">函数</th> <th>描述</th> </tr> <tr> <td>findall</td> <td>返回包含所有匹配项的列表</td> </tr> <tr> <td>search</td> <td>如果文本中有匹配项,则返回匹配对象</td> </tr> <tr> <td>split</td> <td>使用字符串分割文本,返回文本被分割后的列表 </td> </tr> <tr> <td>sub</td> <td>用字符串替换一个或多个匹配项/td> </tr> </tbody></table>
元字符
元字符是具有特殊意义的字符:
<table> <tbody><tr> <th >字符</th> <th>描述</th> <th>例子</th> </tr> <tr> <td>[]</td> <td>字符集合</td> <td>"[a-m]"</td> </tr> <tr> <td>\</td> <td>标志特殊转义字符(也可以用来转义特定字符)</td> <td>"\d"</td> </tr> <tr> <td>.</td> <td>任何字符(换行字符除外)</td> <td>"he..o"</td> </tr> <tr> <td>^</td> <td>开始文本</td> <td>"^hello"</td> </tr> <tr> <td>$</td> <td>结束文本</td> <td>"world$"</td> </tr> <tr> <td>*</td> <td>0次或多次出现</td> <td>"aix*"</td> </tr> <tr> <td>+</td> <td>1次或多次出现</td> <td>"aix+"</td> </tr> <tr> <td>{}</td> <td>确定的出现次数</td> <td>"al{2}"</td> </tr> <tr> <td>|</td> <td>或者</td> <td>"falls|stays"</td> </tr> <tr> <td>()</td> <td>捕获并分组</td> <td> </td> </tr> </tbody></table>
特殊转义字符
特殊转义字符是\
后面跟着下面列表中的某个字符,有特殊的含义:
<table> <tbody><tr> <th>字符</th> <th>描述</th> <th>例子</th> </tr> <tr> <td>\A</td> <td>如果指定字符串位于文本的开头,则返回匹配项</td> <td>"\AThe"</td> </tr> <tr> <td>\b</td> <td>如果指定字符串位于文本开头或结尾,则返回匹配项</td> <td>r"\bain"<br>r"ain\b"</td> </tr> <tr> <td>\B</td> <td>如果指定字符串没有位于文本开头或结尾,则返回匹配项</td> <td>r"\Bain"<br>r"ain\B"</td> </tr> <tr> <td>\d</td> <td>如果文本中包含了数字(0-9),返回匹配项</td> <td>"\d"</td> </tr> <tr> <td>\D</td> <td>如果文本中不包含数字(0-9),返回匹配项</td> <td>"\D"</td> </tr> <tr> <td>\s</td> <td>返回文本中包含空白字符的匹配项</td> <td>"\s"</td> </tr> <tr> <td>\S</td> <td>返回文本中不包含空白字符的匹配项</td> <td>"\S"</td> </tr> <tr> <td>\w</td> <td>如果文本中包含任何单词字符(从a到Z的字符,从0到9的数字,以及下划线_字符),返回匹配项</td> <td>"\w"</td> </tr> <tr> <td>\W</td> <td>如果文本中不包含任何单词字符(从a到Z的字符,从0到9的数字,以及下划线_字符),返回匹配项</td> <td>"\W"</td> </tr> <tr> <td>\Z</td> <td>如果指定的字符串位于文本末尾,则返回匹配项</td> <td>"Spain\Z"</td> </tr> </tbody></table>
集合
集合是一对方括号[]
中的一组字符,具有特殊的含义:
<table> <tbody><tr> <th>Set</th> <th>描述</th> </tr> <tr> <td>[arn]</td> <td>返回匹配指定字符(a、r或n)之一的匹配项</td> </tr> <tr> <td>[a-n]</td> <td>返回匹配任意a和n之间,小写字符的匹配项</td> </tr> <tr> <td>[^arn]</td> <td>返回匹配除a、r和n之外的任何字符的匹配项</td> </tr> <tr> <td>[0123]</td> <td>返回匹配指定数字(0、1、2或3)的匹配项</td> </tr> <tr> <td>[0-9]</td> <td>返回匹配0到9之间任意数字的匹配项</td> </tr> <tr> <td>[0-5][0-9]</td> <td>返回匹配从00到59的任意两位数的匹配项</td> </tr> <tr> <td>[a-zA-Z]</td> <td>返回匹配按字母顺序在a和z之间、小写或大写的任何字符的匹配项</td> </tr> <tr> <td>[+]</td> <td>在集合中,+,*,.,|,(),$,{}没有特殊的含义,所以[+]的意思是: 返回字符串中"+"字符的匹配项</td> </tr> </tbody></table>
findall()函数
findall()
函数返回一个包含所有匹配项的列表。
示例
打印包含所有匹配项的列表:
import re
str = "The rain in Spain"
x = re.findall("ai", str)
print(x)
列表中的匹配项按找到的顺序排序。
如果没有找到匹配项,返回一个空列表:
示例
打印所有匹配项的列表:
import re
str = "The rain in Spain"
x = re.findall("Portugal", str)
print(x)
search() 函数
search()
搜索文本中的匹配项,如果有匹配项,返回匹配对象。
如果有多个匹配项,只返回第一个:
示例
搜索文本中的第一个空白字符:
import re
str = "The rain in Spain"
x = re.search("\s", str)
print("第一个空白字符位于:", x.start())
如果没有找到匹配项,则返回None
值:
示例
不匹配:
import re
str = "The rain in Spain"
x = re.search("Portugal", str)
print(x)
split() 函数
split()
函数使用字符串分割文本,返回文本被分割后的列表:
示例
在每个空白字符处分割文本:
import re
str = "The rain in Spain"
x = re.split("\s", str)
print(x)
可以通过指定maxsplit
参数来控制分割次数:
示例
只在第一次匹配时分割字符串:
import re
str = "The rain in Spain"
x = re.split("\s", str, 1)
print(x)
sub() Function
将匹配项替换为指定文本:
示例
用数字9替换每个空白字符:
import re
str = "The rain in Spain"
x = re.sub("\s", "9", str)
print(x)
可以通过count
参数来控制替换的数量:
示例
替换前两项:
import re
str = "The rain in Spain"
x = re.sub("\s", "9", str, 2)
print(x)
匹配对象
匹配对象是一个包含搜索和结果信息的对象。
注意: 如果没有匹配,则返回None值,而不是匹配对象。
示例
搜索一下,将返回一个匹配对象:
import re
str = "The rain in Spain"
x = re.search("ai", str)
print(x) #打印对象
匹配对象具有用于检索搜索信息的属性和方法:
.span()
返回一个元组,其中包含匹配项的开始和结束位置。.string
返回传递给函数的文本.group()
返回文本中匹配的部分
示例
打印第一个匹配项的位置(开始和结束位置)。
正则表达式查找任何以大写字母“S”开头的单词:
import re
str = "The rain in Spain"
x = re.search(r"\bS\w+", str)
print(x.span())
示例
打印传入函数的文本:
import re
str = "The rain in Spain"
x = re.search(r"\bS\w+", str)
print(x.string)
示例
打印文本中匹配的部分。
正则表达式查找任何以大写字母“S”开头的单词:
import re
str = "The rain in Spain"
x = re.search(r"\bS\w+", str)
print(x.group())
注意: 如果没有匹配,则返回None值,而不是匹配对象。
来源:oschina
链接:https://my.oschina.net/u/4396177/blog/3475914