网络编程
位置:首页>> 网络编程>> Python编程>> Python正则表达式使用经典实例

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

作者:2778085001  发布时间:2022-04-29 01:22:03 

标签: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)
0
投稿

猜你喜欢

  • 我今天晚上,做一个快印公司的网站布局,在Div镶套布局中,父标签DIV的高度不变。在IE下没有问题,但是在FIREFOX下就有问题了。如图:
  • 为最终用户提供的功能主要由一个HTML文件和两个ASP文件提供,它们负责接受用户的订阅申请以及退出邮件列表申请。 用户的个人信息在图1所示的
  • 首先为什么会有axis这个概念?因为在numpy模块中,大多数处理的是矩阵或者多维数组,同时,对多维数组或者矩阵的操作有多种可能,为了帮助实
  • 目前代码应该没什么bug了,兼容IE6.0 & FF 1.5, 通过xHTML 的Transitional验证和 CSS 验证。为了
  • 哎,以前写博文的时候没注意,有些图片用QQ来截取,获得的图片文件名都是类似于QQ截图20120926174732-300×15.png的形式
  • 本文实例为大家分享了Python/C++实现字符串逆序的具体代码,供大家参考,具体内容如下题目描述:将字符串逆序输出Python实现一:借助
  • 前言:通过@语句调用一个函数去给另一个函数增加或修改一些功能的语法规则称之为Python装饰器。下面通过一个小案例来简单的理解什么是装饰器。
  • 许多网站缺乏针对性和友好的导航设计,难以找到连接到相关网页的路径,也没有提供有助于让访客/用户找到所需信息的帮助,用户体验非常糟糕。本期薯片
  • 高阶函数英文叫Higher-order function。什么是高阶函数?我们以实际代码为例子,一步一步深入概念。变量可以指向函数以Pyth
  • 通过百度云API接口抽取得到产品评论的观点,也掠去了很多评论中无用的内容以及符号,为后续进行文本主题挖掘或者规则的提取提供基础。工具 1、百
  • 1、XML 是什么?XML仅仅是一种数据存放格式,这种格式是一种文本(虽然XML规范中也提供了存放二进制数据的解决方案)。事实上有很多文本格
  • 具体代码如下所示:import tkinter as tkimport tkinter.messageboximport copyimpor
  • 数据字典是Oracle存放有关数据库信息的地方,其用途是用来描述数据的。比如一个表的创建者信息,创建时间信息,所属表空间信息,用户访问权限信
  • 在网页设计中有一些通用的交互设计模式。网站导航各种各样的通用和大家熟知的设计模式,可以用来作为为网站创建有效地信息架构的基础。这篇指南涵盖了
  • argparse 是python自带的命令行参数解析包,可以用来方便地读取命令行参数。一、传入一个参数import argpars
  • 本文实例展示了PHP实现的格鲁斯卡尔算法(kruscal)的实现方法,分享给大家供大家参考。相信对于大家的PHP程序设计有一定的借鉴价值。具
  • 实现效果:实现代码import numpy as npfrom skimage import img_as_floatimport matp
  • 1. 折线图折线图(Line Chart)是一种将数据点按照顺序连接起来的图形,也可以看作是将散点图按照X轴坐标顺序链接起来的图形。折线图的
  • 本教程将分步讲解如何使用JQuery和CSS打造一个炫酷动感菜单。jQuery的"write less, do more"
  •         Ajax类   
手机版 网络编程 asp之家 www.aspxhome.com