常见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)

时间: 2024-07-31 07:31:37

常见python正则用法实例的相关文章

Python回调函数用法实例

Python回调函数用法实例 回调函数 “回调函数就是一个通过函数指针调用的函数. 如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用来调用其所指向的函数时,我们就说这是回调函数.” 回调函数就是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用为调用它所指向的函数时,我们就说这是回调函数.回调函数不是由该函数的实现方直接调用,而是在特定的事件或条件发生时由另外的一方调用的,用于对该事件或条件进行响应. 什么是回调 软件模块之间总是存在

Python正则五分彩源码出售用法详解

搞懂 Python 正则表达式用法 作者:枫叶云 来源:见文末 Python 正则表达式 正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配. Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式. re 模块使 Python 语言拥有全部的正则表达式功能.五分彩源码出售(企 娥:217 1793 408) compile 函数根据一个模式字符串和可选的标志参数生成一个正则表达式对象.该对象拥有一系列方法用于正则表达式匹配和替换. r

python 正则模块的使用(re)说明

python 正则模块的使用(re)说明 正则表达式使用反斜杆(\)来转义特殊字符,使其可以匹配字符本身,而不是指定其他特殊的含义.这可能会和python字面意义上的字符串转义相冲突,这也许有些令人费解.比如,要匹配一个反斜杆本身,你也许要用'\\\\'来做为正则表达式的字符串,因为正则表达式要是\\,而字符串里,每个反斜杆都要写成\\. 你也可以在字符串前加上 r 这个前缀来避免部分疑惑,因为 r 开头的python字符串是 raw 字符串,所以里面的所有字符都不会被转义,比如r'\n'这个字

Python正则及geometer正则截图讲解

正则表达式 语法: 1 2 3 4 5 6 import re #导入模块名 p = re.compile("^[0-9]")  #生成要匹配的正则对象 , ^代表从开头匹配,[0-9]代表匹配0至9的任意一个数字, 所以这里的意思是对传进来的字符串进行匹配,如果这个字符串的开头第一个字符是数字,就代表匹配上了 m = p.match('14534Abc')   #按上面生成的正则对象 去匹配 字符串, 如果能匹配成功,这个m就会有值, 否则m为None<br><br

Python高级用法总结

Python很棒,它有很多高级用法值得细细思索,学习使用.本文将根据日常使用,总结介绍Python的一组高级特性,包括:列表推导式.迭代器和生成器.装饰器. 列表推导(list comprehensions) 场景1:将一个三维列表中所有一维数据为a的元素合并,组成新的二维列表. 最简单的方法:新建列表,遍历原三维列表,判断一维数据是否为a,若为a,则将该元素append至新列表中. 缺点:代码太繁琐,对于Python而言,执行速度会变慢很多. 针对场景1,我们首先应该想到用列表解析式来解决处理

C#泛型Dictionary的用法实例详解

本文以实例形式讲述了C#中的泛型Dictionary的用法.具有很好的实用价值.分享给大家供大家参考.具体如下: 泛型最常见的用途是泛型集合,命名空间System.Collections.Generic 中包含了一些基于泛型的集合类,使用泛型集合类可以提供更高的类型安全性,还有更高的性能,避免了非泛型集合的重复的装箱和拆箱. 很多非泛型集合类都有对应的泛型集合类,下面是常用的非泛型集合类以及对应的泛型集合类: 非泛型集合类 泛型集合类 ArrayList List<T> HashTable D

Winform动态加载TabControl用法实例

本文实例讲述了Winform动态加载TabControl用法.分享给大家供大家参考. 具体实现代码如下: 代码如下: private void BindTabData() { dtIPD = new DataTable(); //drItem = new DataTable(); //获取[项目大类]列表显示于 TabPage MRD_Score model = new MRD_Score(); model.ActiveFlag = "Y"; DataTable dtScore = f

Python多重继承用法 Python周末学习

Python多重继承用法 Python周末学习 继承是面向对象编程的一个重要方式,通过继承,子类可以扩展父类的功能,Python也具有该特性,除此之外,Python还可以使用多重继承. 语法: class subClass(Base1,Base2) 该语法的含义是创建了一个subClass类,让它同时继承了Base1和Base2的相关特性,关于继承还有以下规则需要遵循: 1. 继承只会继承父类的方法,不能继承父类的变量: 2. 要想继承父类的变量,需要执行父类的__init__(self)方法:

面试题:JavaIO流分类详解与常用流用法实例

Java流概念: Java把所有的有序数据都抽象成流模型,简化了输入输出,理解了流模型就理解了Java IO.可以把流想象成水流,里面的水滴有序的朝某一方向流动.水滴就是数据,且代表着最小的数据流动单位,在字节流中,水滴就是一字节(byte),在字符流中,水滴就是一字符(char). Java流的分类方法大致分为以下几种: 1.按流向划分,分为输入流.输出流 请注意,这里的流向是以程序的运行时内存为参照的. 输入流类名中包含关键字InputStream或Reader,输出流类名中包含关键字Out