求知 文章 文库 Lib 视频 iPerson 课程 认证 咨询 工具 讲座 Model Center   Code  
会员   
要资料
 
 
 

Python教程
Python快速入门
Python概述
Python环境安装
Python基本语法
Python变量类型
Python基本运算符
Python决策
Python循环
Python数据类型
Python字符串
Python列表
Python元组
Python字典
Python日期时间Date/Time
Python函数
Python模块
Python文件I/O
Python异常处理
高级教程
Python 3开发网络爬虫(一)
Python 3开发网络爬虫(二)
Python 3(三): 伪装浏览器
Python 3(四): 登录
Python面向对象
Python正则表达式
Python CGI编程
 
 

Python正则表达式
966 次浏览
59次  

正则表达式是字符一个特殊的序列,可帮助匹配或者寻找其他的字符串或一组字符串,用一个模式支持一个专门的语法。正则表达式被广泛应用于UNIX世界中。

模块re在Python提供类似Perl的正则表达式全面支持。 re模块引发异常re.error如果在编译或使用正则表达式时发生错误。

我们将涉及两个重要的功能,这将被用于处理的正则表达式。但是首先:有各种各样的字符,当它们在正则表达式中使用,将有特殊的意义。为了避免在处理正则表达式的任何困惑,将使用原始字符串作为r'expression“。

match函数

此函数会尝试重新模式匹配字符串可选标志。

下面是此函数的语法:

re.match(pattern, string, flags=0)

这里的参数的说明:

re.match函数返回成功,失败匹配对象则返回None。我们会group(num) orgroups()函数匹配对象来获得匹配的表达式。

例子:

#!/usr/bin/python
import re

line = "Cats are smarter than dogs"

matchObj = re.match( r'(.*) are (.*?) .*', line, re.M|re.I)

if matchObj:
print "matchObj.group() : ", matchObj.group()
print "matchObj.group(1) : ", matchObj.group(1)
print "matchObj.group(2) : ", matchObj.group(2)
else:
print "No match!!"

当执行上面的代码,它产生以下结果:

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

search函数

此函数将搜索字符串中使用可选的标志第一次出现的RE模式。

下面是此函数语法:

re.search(pattern, string, flags=0)

这里的参数说明:

re.search函数返回成功,没有失败的匹配对象。我们会用group(num) 或groups() 函数来获得匹配的表达式。

例子:

#!/usr/bin/python
import re

line = "Cats are smarter than dogs";

searchObj = re.search( r'(.*) are (.*?) .*', line, re.M|re.I)

if searchObj:
print "searchObj.group() : ", searchObj.group()
print "searchObj.group(1) : ", searchObj.group(1)
print "searchObj.group(2) : ", searchObj.group(2)
else:
print "Nothing found!!"

当执行上面的代码,它产生以下结果:

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

匹配VS搜索:

python提供基于正则表达式两种不同的基本操作:只在字符串的开头匹配检查的匹配,而对于一个匹配字符串中的任何位置搜索检查(这是Perl并在默认情况下)。

例子:

#!/usr/bin/python
import re

line = "Cats are smarter than dogs";

matchObj = re.match( r'dogs', line, re.M|re.I)
if matchObj:
print "match --> matchObj.group() : ", matchObj.group()
else:
print "No match!!"

searchObj = re.search( r'dogs', line, re.M|re.I)
if searchObj:
print "search --> searchObj.group() : ", searchObj.group()
else:
print "Nothing found!!"

当执行上面的代码,产生以下结果:

No match!!
search --> matchObj.group() : dogs

搜索和替换:

一些最重要的re方法,使用正则表达式sub。

语法

re.sub(pattern, repl, string, max=0)

这种方法取代了RE模式字符串中使用的Perl所有匹配,替换所有出现如果提供最大匹配。这个方法将返回修改后的字符串。

例子

下面是一个例子:

#!/usr/bin/python
import re

phone = "2004-959-559 # This is Phone Number"

# Delete Python-style comments
num = re.sub(r'#.*$', "", phone)
print "Phone Num : ", num

# Remove anything other than digits
num = re.sub(r'D', "", phone)
print "Phone Num : ", num

当执行上面的代码,产生以下结果:

Phone Num :  2004-959-559
Phone Num : 2004959559

正则表达式修饰符 - 选项标志

正则表达式字面可以包含一个可选的修饰符来控制匹配的各个方面。修饰符被指定为一个可选的标志。可以使用异或提供多个修饰符(|),如先前所示,并且可以由这些中的一个来表示:

正则表达式模式:

除了控制字符(+ ? . * ^ $ ( ) [ ] { } | ),所有字符匹配自己。可以通过用反斜杠前就转义控制字符。

下表列出了Python中可用正则表达式语法:


您可以捐助,支持我们的公益事业。

1元 10元 50元





认证码: 验证码,看不清楚?请点击刷新验证码 必填



966 次浏览
59次
 捐助