Python 的 re 模块(Regular Expression 正则表达式)提供各种正则表达式的匹配操作,在文本解析、复杂字符串分析和信息提取时是一个非常有用的工具,下面我主要总结了re的常用方法。

1.re的简介
    使用python的re模块,尽管不能满足所有复杂的匹配情况,但足够在绝大多数情况下能够有效地实现对复杂字符串的分析并提取出相关信息。python 会将正则表达式转化为字节码,利用 C 语言的匹配引擎进行深度优先的匹配。

  1. import re

  2. print re.__doc__

    可以查询re模块的功能信息,下面会结合几个例子说明。

2.re的正则表达式语法
    正则表达式语法表如下:

语法意义说明
"."任意字符
"^"字符串开始'^hello'匹配'helloworld'而不匹配'aaaahellobbb'
"$"字符串结尾与上同理
"*" 0 个或多个字符(贪婪匹配)<*>匹配<title>chinaunix</title>
"+"1 个或多个字符(贪婪匹配与上同理
"?"0 个或多个字符(贪婪匹配与上同理
*?,+?,??以上三个取第一个匹配结果(非贪婪匹配<*>匹配<title>
{m,n}对于前一个字符重复m到n次,{m}亦可a{6}匹配6个a、a{2,4}匹配2到4个a
{m,n}?对于前一个字符重复m到n次,并取尽可能少‘aaaaaa’中a{2,4}只会匹配2个
"\\"特殊字符转义或者特殊序列
[]表示一个字符集[0-9]、[a-z]、[A-Z]、[^0]
"|"A|B,或运算
(...)匹配括号中任意表达式
(?#...)注释,可忽略
(?=...)Matches if ... matches next, but doesn't consume the string.'(?=test)'  在hellotest中匹配hello
(?!...)Matches if ... doesn't match next.'(?!=test)'  若hello后面不为test,匹配hello
(?<=...) Matches if preceded by ... (must be fixed length).'(?<=hello)test'  在hellotest中匹配test
(?<!...)Matches if not preceded by ... (must be fixed length).'(?<!hello)test'  在hellotest中不匹配test

    正则表达式特殊序列表如下:

特殊序列符号意义
\A只在字符串开始进行匹配
\Z只在字符串结尾进行匹配
\b匹配位于开始或结尾的空字符串
\B匹配不位于开始或结尾的空字符串
\d相当于[0-9]
\D相当于[^0-9]
\s匹配任意空白字符:[\t\n\r\r\v]
\S匹配任意非空白字符:[^\t\n\r\r\v]
\w匹配任意数字和字母:[a-zA-Z0-9]
\W匹配任意非数字和字母:[^a-zA-Z0-9]

3.re的主要功能函数
    常用的功能函数包括:compile、search、match、split、findall(finditer)、sub(subn)
compile
re.compile(pattern[, flags])
作用:把正则表达式语法转化成正则表达式对象
flags定义包括:
re.I:忽略大小写
re.L:表示特殊字符集 \w, \W, \b, \B, \s, \S 依赖于当前环境
re.M:多行模式
re.S:’ . ’并且包括换行符在内的任意字符(注意:’ . ’不包括换行符)
re.U: 表示特殊字符集 \w, \W, \b, \B, \d, \D, \s, \S 依赖于 Unicode 字符属性数据库
更多用法可以在http://www.devexception.com/sitemap_index.xml上查找
search
re.search(pattern, string[, flags])
search (string[, pos[, endpos]])
作用:在字符串中查找匹配正则表达式模式的位置,返回 MatchObject 的实例,如果没有找到匹配的位置,则返回 None。

match
re.match(pattern, string[, flags])
match(string[, pos[, endpos]])
作用:match() 函数只在字符串的开始位置尝试匹配正则表达式,也就是只报告从位置 0 开始的匹配情况,而 search() 函数是扫描整个字符串来查找匹配。如果想要搜索整个字符串来寻找匹配,应当用 search()。

下面是几个例子:
例:最基本的用法,通过re.RegexObject对象调用

  1. #!/usr/bin/env python

  2. import re


  3. r1 = re.compile(r'world')

  4. if r1.match('helloworld'):

  5.     print 'match succeeds'

  6. else:

  7.     print 'match fails'


  8. if r1.search('helloworld'):

  9.     print 'search succeeds'

  10. else:

  11.     print 'search fails'

说明一下:r是raw(原始)的意思。因为在表示字符串中有一些转义符,如表示回车'\n'。如果要表示\表需要写为'\\'。但如果我就是需要表示一个'\'+'n',不用r方式要写为:'\\n'。但使用r方式则为r'\n'这样清晰多了。

例:设置flag

  1. #r2 = re.compile(r'n$', re.S)

  2. #r2 = re.compile('\n$', re.S)

  3. r2 = re.compile('World$', re.I)

  4. if r2.search('helloworld\n'):

  5.     print 'search succeeds'

  6. else:

  7.     print 'search fails'


例:直接调用

  1. if re.search(r'abc','helloaaabcdworldn'):

  2.     print 'search succeeds'

  3. else:

  4.     print 'search fails'

split
re.split(pattern, string[, maxsplit=0, flags=0])
split(string[, maxsplit=0])
作用:可以将字符串匹配正则表达式的部分割开并返回一个列表
例:简单分析ip

  1. #!/usr/bin/env python

  2. import re

  3. r1 = re.compile('W+')

  4. print r1.split('192.168.1.1')

  5. print re.split('(W+)','192.168.1.1')

  6. print re.split('(W+)','192.168.1.1',
    1)

结果如下:
['192', '168', '1', '1']
['192', '.', '168', '.', '1', '.', '1']
['192', '.', '168.1.1']

findall
re.findall(pattern, string[, flags])
findall(string[, pos[, endpos]])
作用:在字符串中找到正则表达式所匹配的所有子串,并组成一个列表返回
例:查找[]包括的内容(贪婪和非贪婪查找)

  1. #!/usr/bin/env python

  2. import re


  3. r1 = re.compile('([.*])')

  4. print re.findall(r1,"hello[hi]heldfsdsf[iwonder]lo")

  5. r1 = re.compile('([.*?])')

  6. print re.findall(r1,"hello[hi]heldfsdsf[iwonder]lo")


  7. print re.findall('[0-9]{2}',"fdskfj1323jfkdj")

  8. print re.findall('([0-9][a-z])',"fdskfj1323jfkdj")

  9. print re.findall('(?=www)',"afdsfwwwfkdjfsdfsdwww")

  10. print re.findall('(?<=www)',"afdsfwwwfkdjfsdfsdwww")

finditer
re.finditer(pattern, string[, flags])
finditer(string[, pos[, endpos]])
说明:和 findall 类似,在字符串中找到正则表达式所匹配的所有子串,并组成一个迭代器返回。同样 RegexObject 有:

sub
re.sub(pattern, repl, string[, count, flags])
sub(repl, string[, count=0])
说明:在字符串 string 中找到匹配正则表达式 pattern 的所有子串,用另一个字符串 repl 进行替换。如果没有找到匹配 pattern 的串,则返回未被修改的 string。Repl 既可以是字符串也可以是一个函数。
例:

  1. #!/usr/bin/env python

  2. import re


  3. = re.compile('(one|two|three)')

  4. print p.sub('num','one word two words three words
    apple'
    , 2)

subn
re.subn(pattern, repl, string[, count, flags])
subn(repl, string[, count=0])
说明:该函数的功能和 sub() 相同,但它还返回新的字符串以及替换的次数。

re模块匹配时贪婪和非贪婪模式

python贪婪和非贪婪

  正则表达式通常用于在文本中查找匹配的字符串。Python里数量词默认是贪婪的(在少数语言里也可能是默认非贪婪),总是尝试匹配尽可能多的字符;非贪婪则相反,总是尝试匹配尽可能少的字符。在"*","?","+","{m,n}"后面加上?,使贪婪变成非贪婪。

>>> s="This is a number 234-235-22-423"
>>> r=re.match(".+(\d+-\d+-\d+-\d+)",s)
>>> r.group(1)
'4-235-22-423'
>>> r=re.match(".+?(\d+-\d+-\d+-\d+)",s)
>>> r.group(1)
'234-235-22-423'
>>>

  正则表达式模式中使用到通配字,那它在从左到右的顺序求值时,会尽量“抓取”满足匹配最长字符串,在我们上面的例子里面,“.+”会从字符 串的启始处抓取满足模式的最长字符,其中包括我们想得到的第一个整型字段的中的大部分,“\d+”只需一位字符就可以匹配,所以它匹配了数字“4”,而“.+”则匹配了从字符串起始到这个第一位数字4之前的所有字符。

  解决方式:非贪婪操作符“?”,这个操作符可以用在"*","+","?"的后面,要求正则匹配的越少越好。

 

  下面这个例子仔细体会下

>>> re.match(r"aa(\d+)","aa2343ddd").group(1)
'2343'
>>> re.match(r"aa(\d+?)","aa2343ddd").group(1)
'2'
>>> re.match(r"aa(\d+)ddd","aa2343ddd").group(1) 
'2343'
>>> re.match(r"aa(\d+?)ddd","aa2343ddd").group(1)
'2343'
>>>

本文出自http://blog.csdn.net/tangdong3415/article/details/54583337

婪模式是把所有匹配的获取到,非贪婪模式只取到第一个匹配到的字符串,在python中类似于findall和match的区别。

.*与.*?的区别:

import re

secret_code = 'hadkfalifexxIxxfasdjifja134xxlovexx23345sdfxxyouxx8dfse'

b = re.findall('xx.*xx',secret_code)

print (b)

c = re.findall('xx.*?xx',secret_code)

print(c)

实验结果:

['xxIxxfasdjifja134xxlovexx23345sdfxxyouxx'] ['xxIxx', 'xxlovexx', 'xxyouxx']