9.python中的正则表达式

#-*-coding:UTF-8-*-
s='100 BROAD'

import re

print re.sub('ROAD$','RD.',s)           #^匹配字符串开始,$匹配字符串结尾

print re.sub(r'\bROAD$','RD.',s)       #\b表示这里必须要有一个分隔符,字符串前面的r表示后面字符串中的内容不需要转义

print re.search('ROAD$',s)              #search()函数,它使用正则表达式来匹配字符串,匹配成功,返回一个匹配对象,没有匹配到则返回None

a=' '
print re.search('ROAD$',a)

原文地址:https://www.cnblogs.com/chenjianhong/p/4145135.html