首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

正则表达式有关问题

2012-02-25 
正则表达式问题《核心编程2》中15.3.10节: m re.search(r\bthe, bite the dog) # at a boundary#在

正则表达式问题
《核心编程2》中15.3.10节:
>>> m = re.search(r'\bthe', 'bite the dog') # at a boundary #在词边界 
>>> if m is not None: m.group()
请问这个r是什么意思?不写r的话m竟为None


>>> m = re.search('^The', 'The end.') # match #匹配 
>>> if m is not None: m.group() 
... 
'The' 
^The不是匹配以The开头的字符串‘The end’吗?结果怎么会是'The' ?

[解决办法]
r''表示原意字符串

^The它只能匹配以The开头的字符串中的The, 
^The.*才会匹配The end.
[解决办法]
当你去掉r时,会把\b当成ascii的退格键,所以写成\\b就可以了,相当r \b

热点排行