正则表达式

正则表达式
        正则表达式是一种用来匹配字符串的强有力的武器。
它的设计思想是用一种描述性的语言来给字符串定义一
个规则,凡是符合规则的字符串,我们就认为它“匹配”了,
否则,该字符串就是不合法的。

基本模式
• 字面模式: 就是字面长量,就代表其本身
• . 匹配任何字符
• \w 匹配一个单词(字母) \W 匹配非字母
• \s 匹配空白 \S 匹配非空白字符
• \d 匹配数字
• ^ 开头 $ 结尾
• \ 转义字符

反斜杠后面可以加不同字符以表示不同特殊意义, 也可以用于取消所有的元字符: \[ 或 \\
    \d        匹配任何十进制数,类似[0-9]
    \D        匹配任何非数字字符,类似[^0-9]
    \s        匹配任何空白字符,类似[\t\n\r\f\v]
    \S        匹配任何非空白字符,类似[^\t\n\r\f\v]
    \w        匹配任何字母数字字符,类似[a-zA-Z0-9_]
    \W        匹配任何非字母数字字符,类似[^a-zA-Z0-9_]


 字面模式
In [14]: r = 'yu\\n'
In [15]: s = 'di wu ling yu\\n'
In [16]: import re
In [17]: re.findall(r,s)
Out[17]: []
In [18]: r1 = r'yu\\n'
In [19]: re.findall(r1,s)
Out[19]: ['yu\\n']
判断一个字符串是否合法的Email的方法
In [20]: r = r'\w+@[0-9a-zA-Z]+\.cn|\w+@[0-9a-zA-Z]+\.org'
In [21]: r = r'[0-9a-zA-Z\_]+@[0-9a-zA-Z]+\.cn|[0-9a-zA-Z\_]+@[0-9a-zA-Z]+\.org'

In [25]: s = '762__@jgij211.cn'
In [26]: re.findall(r,s)
Out[26]: ['762__@jgij211.cn']

判断满足029-1234567这样要求的电话号码的方法
In [2]: r = r'^\d{3}-^\d{7}'
In [3]: s = '321-3621881391'

In [4]: re.findall(r,s)
Out[4]: []

In [5]: s = '3221-3621891'

In [6]: re.findall(r,s)
Out[6]: []

判断一个变量名是否合法
In [7]: r = r'^[_a-zA-Z]\w*$'

次数的匹配
次数的匹配 , 匹配其前面的字符出现的次数 :
• * 0 次或多次
• + 一次或多次
• ? 零次或一次
• {n} 出现 n 次
• {m,n} 出现 m 到 n 次

中括号
• 中括号用于指向一个字符集合
• 中括号可以使用元字符
• 中括号中的. 表示其字面意思
[a-z] [A-Z] [0-9] [A-Za-z]

中括号的用法
• [0-9a-zA-Z\_] 可以匹配一个数字、字母或者下划线;
• [0-9a-zA-Z\_]+ 可以匹配至少由一个数字、字母或者下划线组成的字
符串;
• [a-zA-Z\_][0-9a-zA-Z\_]{0, 19} 更精确地限制了变量的长度是 1­20
个字符;
•A|B 可以匹配 A 或 B
•^\d 表示必须以数字开头
• \d$ 表示必须以数字结束


re 模块
r = r'hello'
re.match(r, 'hello')
re.match(r, 'westos')
match() 方法判断是否匹配,如果匹配成功,返回一个 Match
对象,否则返回 None 。

编译
当我们在 Python 中使用正则表达式时,re 模块内部会干两件事情:
1. 编译正则表达式,如果正则表达式的字符串本身不合法,会报错;
2. 用编译后的正则表达式去匹配字符串。
重复使用几千次,出于效率的考虑,我们可以预编译该正则表达式。

In [1]: import re

In [2]: r = r't.p'

In [3]: p = re.compile(r)

In [5]: re.findall(p,'top tap tab')    ##匹配满足规则的字符串
Out[5]: ['top', 'tap']

In [6]: a = re.match(p,'top tap tab')   ##match方法判断是否匹配

In [7]: a.group()      #返回一个match对象
Out[7]: 'top'

In [8]: a.start()      #match开始匹配的索引
Out[8]: 0

In [9]: a.end()
Out[9]: 3

In [10]: a.span()
Out[10]: (0, 3)

In [1]: import re

In [2]: a = re.match(r'hello','westos hello westos')    #方法的地址赋给了变量,在使用match方法时,只去匹配第一个

In [3]: a.group()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-af218c045ead> in <module>()
----> 1 a.group()

AttributeError: 'NoneType' object has no attribute 'group'

In [5]: if a:       #判断是否有匹配
    print 'Match found:%s' %a.group()
else:
    print 'no match'
   ...:
no match             #无匹配结果

In [6]: a = re.search(r'hello','westos hello westos')    #在使用search方法时,将会遍历所有去匹配

In [7]: a.group()
Out[7]: 'hello'

In [8]: a = re.search(r'hello','hello westos')

In [9]: a.group()
Out[9]: 'hello'


总结
• re.match(p,text) :p 为正则表达式模式, text 要查找的字符串,会返回一个
match 对象
• re.search(p,text) : 只要在 text 中匹配到了 p 就返回,只返回第一个匹配到

• re.findall(p,text) :将能匹配上的全返回,会返回一个 list
• re.split(p,text) : 按照 p 匹配,并且以匹配到的字符为分隔符切割 text, 返回
一个切割后的 list
• re.sub(p,s,text) : 替换,将 p 匹配到的字符替换为 s.
• pattern = re.compile(p) 先编译 p 模式,当正则表达式模式比较复杂的时候,
会先编译,然后再使用

re模块中方法的使用:

In [13]: a = re.finditer(r'hello','hello westos')

In [14]: a.next()
Out[14]: <_sre.SRE_Match at 0x304af38>

In [16]: a = re.finditer(r'hello','hello westos')

In [17]: a.next().group()
Out[17]: 'hello'

字符串中replace方法替换指定字符
In [18]: s = 'hello westos'

In [19]: s.replace('westos','fentiao')
Out[19]: 'hello fentiao'

在‘hello westos’中,将正则表达式匹配到的字符,替换成fentiao
In [20]: re.sub(r'wes..s','fentiao','hello westos')
Out[20]: 'hello fentiao'

统计替换的次数
In [21]: re.subn(r'wes..s','fentiao','hello westos')
Out[21]: ('hello fentiao', 1)

In [22]: re.subn(r'wes..s','fentiao','hello westos westos')
Out[22]: ('hello fentiao fentiao', 2)

re.split(p,text) 按照 p 匹配,并且以匹配到的字符为分隔符切割 text, 返回
一个切割后的 list 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值