您的位置:首页 > 编程语言 > Python开发

python核心编程-正则表达式之-边界匹配

2016-01-13 22:13 761 查看
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import re

#在开头
m = re.search('^The', 'The end.')
if m is not None:
print m.group()
print '1>>>>>>>>>>>>>'

#不在开头
m = re.search('^The', 'end. The')
if m is not None:
print m.group()
print '2>>>>>>>>>>>>>'

#\b匹配边界词
m = re.search(r'\bthe', 'bite the dog')
if m is not None:
print m.group()
print '3>>>>>>>>>>>>>'

m = re.search(r'\bthe', 'bitethe dog')
if m is not None:
print m.group()
print '4>>>>>>>>>>>>>'

#\B 是 \b的反义词
m = re.search(r'\Bthe', 'bitethe dog')
if m is not None:
print m.group()
print '5>>>>>>>>>>>>>'


输出:

D:\Python27\test>re07.py

The

1>>>>>>>>>>>>>

2>>>>>>>>>>>>>

the

3>>>>>>>>>>>>>

4>>>>>>>>>>>>>

the

5>>>>>>>>>>>>>

D:\Python27\test>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: