Python正则表达式使用经典实例

 更新时间:2016年06月21日 09:59:23   作者:2778085001  
本文给大家总结了17种python正则表达式使用经典实例,非常不错具有参考借鉴价值,感兴趣的朋友一起学习吧

下面列出Python正则表达式的几种匹配用法,具体内容如下所示:

此外,关于正则的一切http://deerchao.net/tutorials/regex/regex.htm

1.测试正则表达式是否匹配字符串的全部或部分

regex=ur"" #正则表达式
if re.search(regex, subject):
do_something()
else:
do_anotherthing()

2.测试正则表达式是否匹配整个字符串

regex=ur"\Z" #正则表达式末尾以\Z结束
if re.match(regex, subject):
    do_something()
else:
    do_anotherthing()

3.创建一个匹配对象,然后通过该对象获得匹配细节(Create an object with details about how the regex matches (part of) a string)

regex=ur"" #正则表达式
match = re.search(regex, subject)
if match:
    # match start: match.start()
    # match end (exclusive): atch.end()
    # matched text: match.group()
    do_something()
else:
    do_anotherthing()

4.获取正则表达式所匹配的子串(Get the part of a string matched by the regex)

regex=ur"" #正则表达式
match = re.search(regex, subject)
if match:
    result = match.group()
else:
    result = ""

5. 获取捕获组所匹配的子串(Get the part of a string matched by a capturing group)

regex=ur"" #正则表达式
match = re.search(regex, subject)
if match:
    result = match.group(1)
else:
    result = ""

6. 获取有名组所匹配的子串(Get the part of a string matched by a named group)

regex=ur"" #正则表达式
match = re.search(regex, subject)
if match:
result = match.group"groupname")
else:
result = ""

7. 将字符串中所有匹配的子串放入数组中(Get an array of all regex matches in a string)

result = re.findall(regex, subject)

8.遍历所有匹配的子串(Iterate over all matches in a string)

for match in re.finditer(r"<(.*?)\s*.*?/\1>", subject)
&nbsp;&nbsp;&nbsp;&nbsp;# match start: match.start()
&nbsp;&nbsp;&nbsp;&nbsp;# match end (exclusive): atch.end()
&nbsp;&nbsp;&nbsp;&nbsp;# matched text: match.group()

9.通过正则表达式字符串创建一个正则表达式对象(Create an object to use the same regex for many operations)

reobj = re.compile(regex)

10.用法1的正则表达式对象版本(use regex object for if/else branch whether (part of) a string can be matched)

reobj = re.compile(regex)
if reobj.search(subject):
&nbsp;&nbsp;&nbsp;&nbsp;do_something()
else:
&nbsp;&nbsp;&nbsp;&nbsp;do_anotherthing()

11.用法2的正则表达式对象版本(use regex object for if/else branch whether a string can be matched entirely)

reobj = re.compile(r"\Z") #正则表达式末尾以\Z 结束
if reobj.match(subject):
&nbsp;&nbsp;&nbsp;&nbsp;do_something()
else:
&nbsp;&nbsp;&nbsp;&nbsp;do_anotherthing()

12.创建一个正则表达式对象,然后通过该对象获得匹配细节(Create an object with details about how the regex object matches (part of) a string)

reobj = re.compile(regex)
match = reobj.search(subject)
if match:
&nbsp;&nbsp;&nbsp;&nbsp;# match start: match.start()
&nbsp;&nbsp;&nbsp;&nbsp;# match end (exclusive): atch.end()
&nbsp;&nbsp;&nbsp;&nbsp;# matched text: match.group()
&nbsp;&nbsp;&nbsp;&nbsp;do_something()
else:
&nbsp;&nbsp;&nbsp;&nbsp;do_anotherthing()

13.用正则表达式对象获取匹配子串(Use regex object to get the part of a string matched by the regex)

reobj = re.compile(regex)
match = reobj.search(subject)
if match:
&nbsp;&nbsp;&nbsp;&nbsp;result = match.group()
else:
&nbsp;&nbsp;&nbsp;&nbsp;result = ""

14.用正则表达式对象获取捕获组所匹配的子串(Use regex object to get the part of a string matched by a capturing group)

reobj = re.compile(regex)
match = reobj.search(subject)
if match:
&nbsp;&nbsp;&nbsp;&nbsp;result = match.group(1)
else:
&nbsp;&nbsp;&nbsp;&nbsp;result = ""

15.用正则表达式对象获取有名组所匹配的子串(Use regex object to get the part of a string matched by a named group)

reobj = re.compile(regex)
match = reobj.search(subject)
if match:
&nbsp;&nbsp;&nbsp;&nbsp;result = match.group("groupname")
else:
&nbsp;&nbsp;&nbsp;&nbsp;result = ""

16.用正则表达式对象获取所有匹配子串并放入数组(Use regex object to get an array of all regex matches in a string)

reobj = re.compile(regex)
result = reobj.findall(subject)

17.通过正则表达式对象遍历所有匹配子串(Use regex object to iterate over all matches in a string)

reobj = re.compile(regex)
for match in reobj.finditer(subject):
&nbsp;&nbsp;&nbsp;&nbsp;# match start: match.start()
&nbsp;&nbsp;&nbsp;&nbsp;# match end (exclusive): match.end()
&nbsp;&nbsp;&nbsp;&nbsp;# matched text: match.group()

字符串替换

1.替换所有匹配的子串

#用newstring替换subject中所有与正则表达式regex匹配的子串
result = re.sub(regex, newstring, subject)

2.替换所有匹配的子串(使用正则表达式对象)

reobj = re.compile(regex)
result = reobj.sub(newstring, subject)

字符串拆分

1.字符串拆分

result = re.split(regex, subject)

2.字符串拆分(使用正则表示式对象)

reobj = re.compile(regex)
result = reobj.split(subject)

相关文章

  • Sanic框架安装与简单入门示例

    Sanic框架安装与简单入门示例

    这篇文章主要介绍了Sanic框架安装与简单用法,结合实例形式简单分析了Sanic框架的概念、原理、pip命令安装以及使用方法,需要的朋友可以参考下
    2018-07-07
  • Python实现自动添加脚本头信息的示例代码

    Python实现自动添加脚本头信息的示例代码

    这篇文章给大家介绍的一段脚本是自动添加注释信息的脚本,添加的信息包括脚本名称、作者和时间等之类的,对于团队形成统一的编码规则很有帮助。有需要的可以参考借鉴。
    2016-09-09
  • Python实现双色球号码随机生成

    Python实现双色球号码随机生成

    和体彩大乐透类似,福彩双色球也是购买次数最多的彩种之一,相比大乐透,双色球更容易中小奖。本文将介绍 Python 实习双色球彩票自由的流程,感兴趣的可以了解一下
    2022-05-05
  • Python统计纯文本文件中英文单词出现个数的方法总结【测试可用】

    Python统计纯文本文件中英文单词出现个数的方法总结【测试可用】

    这篇文章主要介绍了Python统计纯文本文件中英文单词出现个数的方法,结合实例形式总结分析了Python针对文本文件的读取,以及统计文本文件中英文单词个数的4种常用操作技巧,需要的朋友可以参考下
    2018-07-07
  • Python实现自动回复QQ消息功能的示例代码

    Python实现自动回复QQ消息功能的示例代码

    这篇文章主要为大家详细介绍了如何利用Python语言实现自动回复QQ消息功能,文中的示例代码讲解详细,具有一定的借鉴价值,需要的可以参考一下
    2022-10-10
  • Python曲线平滑的实现示例

    Python曲线平滑的实现示例

    本文主要介绍了Python曲线平滑的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-05-05
  • 我用Python抓取了7000 多本电子书案例详解

    我用Python抓取了7000 多本电子书案例详解

    这篇文章主要介绍了我用Python抓取了7000 多本电子书案例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-03-03
  • Pandas之MultiIndex对象的示例详解

    Pandas之MultiIndex对象的示例详解

    这篇文章主要介绍了Pandas之MultiIndex对象的示例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-06-06
  • 利用python实现短信和电话提醒功能的例子

    利用python实现短信和电话提醒功能的例子

    今天小编就为大家分享一篇利用python实现短信和电话提醒功能的例子,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-08-08
  • python求最大值,不使用内置函数的实现方法

    python求最大值,不使用内置函数的实现方法

    今天小编就为大家分享一篇python求最大值,不使用内置函数的实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-07-07

最新评论