正则表达式

正则表达式

使用正则表达式匹配;与之相关的功能位于标准库模块re.
简单示例:

>>> result=re.match('You','Young Fankenstenin')
这里’You’是模式,’Young Fankenstenin’是源。Match()函数用来检查源是否以模式开头
对于更加复杂的匹配,可以先对模式进行编译以加快匹配速度
>>> youpattern=re.compile('You')
>>> result=youpattern.match('You Fafankstein')

【匹配方法】
1)search() 会返回第一次成功匹配,如果存在的话
2)findall() 会返回所有成功的匹配,如果存在的话
3)Split() 会根据pattern将源切分成若干段,返回由这些片段组成的列表
4)Sub() 还需一个额外的参数replacement它会把source中所有匹配的pattern改成replacement,

使用match()函数匹配

1)只能检测以模式串作为开头的源字符串但是search()可以检测任何位置的匹配

>>> import re
>>> source='Young Frankenstein'
>>> m=re.match('You',source)
>>> if m:
...     print (m.group())
... 
You
>>> m=re.match('^You',source)
>>> if m:
...     print (m.group())
... 
You

改变一下模式匹配

>>> m=re.match('.*Frank',source)
>>> if m:
...     print (m.group())
... 
Young Frank

简单解释:
. 代表任何一个单字符

  • 代表任意一个它之前的字符
    .* 代表任意多个字符(包括0个

使用search()寻找首次匹配

使用search()函数在源字符串‘Young Frankstein’的任意字符寻找模式‘Frank’,无需通配符.*

>>> m=re.match('Frank',source)
>>> if m:
...     print (m.group())  
...   #什么也没有返回
>>> m=re.search('Frank',source)
>>> if m: #返回字符串
...     print (m.group())  
... 
Frank

使用findall()寻找所有匹配

之前的例子都是查找到一个匹配以后就停止了,但如果想知道一个字符串中出现了多少次字母‘n’应该怎么办

>>> m=re.findall('n',source)
>>> m     #m返回一个列表
['n', 'n', 'n', 'n']
>>> print ('Found',len(m),'matches')
('Found', 4, 'matches')
将模式改成n,紧跟着一个任意字符,
>>> m=re.findall('n.',source)
>>> m
['ng', 'nk', 'ns']
上面的例子中最后一个n并没有匹配成功,需要通过?说明n后面的字符是可选的‘
>>> m=re.findall('n.?',source)
>>> m
['ng', 'nk', 'ns', 'n']

使用split()按匹配来切分

将一个字符串切分成由一系列子串组成的列表

>>> source
'Young Frankenstein'
>>> m=re.split('n',source)
>>> m
['You', 'g Fra', 'ke', 'stei', '']

使用sub()替换匹配

>>> m=re.sub('n','**',source)
>>> m
'You**g Fra**ke**stei**'

模式,特殊的字符

一些基本的模式
使用. 代表除\n外的字符
使用*代表任意多个字符(包括0个)
使用?表示可选字符(0个或1个)
特殊的字符
\d 一个数字字符
\D 一个非数字字符
\w 一个数字或字母字符
\W 一个非字母非数字字符
\s 空白符
\S 非空白符
\b 单词边界
\B 非单词边界

Python中的string模块定义了我们测试用的字符串常量,我们使用printable字符串,它包含了100个可打印的ASCII字符

>>> import string
>>> printable=string.printable
>>> len(printable)
100
>>> printable[0:30]
'0123456789abcdefghijklmnopqrst'
>>> printable[50:]
'OPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'

>>> re.findall('\d',printable) #哪些是数字
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
>>> re.findall('\w',printable)#哪些是数字,字母,下划线
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '_']
>>> re.findall('\s',printable)哪些是空格符
[' ', '\t', '\n', '\r', '\x0b', '\x0c']

模式:使用标识符

abc 文本值abc
(expr) expr
expr1 | expr2 expr1或expr2
. 除\n外的任何字符
^ 源字符串的开头
$ 源字符串的结尾
Prcv? 0个或1个prcv
Prcv* 0个多都个prcv,尽可能多的匹配
Prcv*? 0个或多个prcv,尽可能少的匹配
Prcv+ 1个或多个prcv,尽可能多的匹配
Prcv+? 1个或多个prcv,尽可能少的匹配
Prcv{m} m个连续的prcv
Prcv{m,n} m到n个连续的prcv,尽可能多的匹配
Prcv{m,n}? M到n个连续的prcv,尽可能少的匹配
[abc] a或b或c
[1] 非(a或b或c)
Prcv(?=next) 如果后面为next返回prcv
Prcv(?!=next) 如果后面非next,返回prcv
(?<=prcv)next 如果前面位为prcv,返回next
(?<!=prcv) next如果前面非prcv,返回next
例子

>>> source='''I wish I may, I wish I might
... Have  a dish of fish tonight.'''
>>> re.findall('wish',source) #在源字符串中找wish
['wish', 'wish']
>>> re.findall('wish|fish',source) 在源字符串中找wish或者fish
['wish', 'wish', 'fish']
>>> re.findall('^wish',source) 从字符串开头匹配wish
[]
>>> re.findall('^I wish',source)从字符串开头开始匹配wish
['I wish']
>>> re.findall('fish$',source) 从字符串结尾匹配fish
[]
>>> re.findall('fish tonight.$',source) 从字符串结尾匹配fish tonight.
['fish tonight.']
.应该更准确的使用转义字符,
>>> re.findall('fish tonight\.$',source)
['fish tonight.']

接下来搜寻以w或f,后面紧跟着ish

>>> re.findall('[wf]ish',source) 
['wish', 'wish', 'fish']
>>> re.findall('[wsh]+',source) 查询以若干个w,s,h组合的匹配
['w', 'sh', 'w', 'sh', 'h', 'sh', 'sh', 'h']
>>> re.findall('ght\W',source)查询以ght开头,后面紧跟着一个非数字的匹配
['ght\n', 'ght.']

查询以I 开头,后面跟着wish的匹配,(wish 出现的字符尽量少)

>>> re.findall('I (?=wish)',source)
['I ', 'I ']
查询以wish 结尾的,前面为I的匹配(I出现的次数尽量少)
>>> re.findall('(?<=I) wish',source)
[' wish', ' wish']

查询以fish开头的词

>>> re.findall('\bfish',source)
[]

与python 本身语法冲突,\b在字字符串中代表退格,但在正则表达式中代表一个单词开头的位置。在模式字符串中前面添加字符r,告诉python这是一个正则表达式

>>> re.findall(r'\bfish',source)
['fish']

模式:定义匹配的输出

当使用match()或search()时,所有的匹配会以m.group()的形式返回到对象m中,如果你i使用括号将某一模式包裹起来,括号中的模式匹配得到的结果归入自己的group(无名称)中,而调用m.groups()可以得到包含这些匹配的元祖

>>> m=re.search(r'(. dish\b).*(\bfish)',source)
>>> m.group()
'a dish of fish'
>>> m.groups()
('a dish', 'fish'

  1. abc ↩︎

posted @ 2016-07-25 17:59  夏日花开  阅读(156)  评论(0编辑  收藏  举报