上一篇文章為:→5.1.5表示數(shù)量
表示邊界
字符 | 功能 |
---|---|
^ | 匹配字符串開頭 |
$ | 匹配字符串結(jié)尾 |
\b | 匹配一個單詞的邊界 |
\B | 匹配非單詞邊界 |
示例1:$
需求:匹配163.com的郵箱地址
#coding=utf-8
import re
# 正確的地址
ret = re.match("[\w]{4,20}@163\.com", "xiaoWang@163.com")
ret.group()
# 不正確的地址
ret = re.match("[\w]{4,20}@163\.com", "xiaoWang@163.comheihei")
ret.group()
# 通過$來確定末尾
ret = re.match("[\w]{4,20}@163\.com$", "xiaoWang@163.comheihei")
ret.group()
運行結(jié)果:
示例2: \b
>>> re.match(r".*\bver\b", "ho ver abc").group()
'ho ver'
>>> re.match(r".*\bver\b", "ho verabc").group()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'group'
>>> re.match(r".*\bver\b", "hover abc").group()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'group'
>>>
示例3:\B
>>> re.match(r".*\Bver\B", "hoverabc").group()
'hover'
>>> re.match(r".*\Bver\B", "ho verabc").group()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'group'
>>> re.match(r".*\Bver\B", "hover abc").group()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'group'
>>> re.match(r".*\Bver\B", "ho ver abc").group()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'group'