#!/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>