读书人

正则表达式有关问题

发布时间: 2012-02-25 10:01:49 作者: rapoo

正则表达式问题
《核心编程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

读书人网 >perl python

热点推荐