我有一个带有 python doctest 的函数,该函数失败,因为其中一个测试输入字符串有一个反斜杠,即使我已将字符串编码为原始字符串,它也被视为转义字符。
我的 doctest 看起来像这样:
>>> infile = [ "Todo: fix me", "/** todo: fix", "* me", "*/", r"""//\todo stuff to fix""", "TODO fix me too", "toDo bug 4663" ]
>>> find_todos( infile )
['fix me', 'fix', 'stuff to fix', 'fix me too', 'bug 4663']
该函数旨在根据待办事项规范的一些变化从单行中提取待办事项文本,如下所示:
todos = list()
for line in infile:
print line
if todo_match_obj.search( line ):
todos.append( todo_match_obj.search( line ).group( 'todo' ) )
并且调用的正则表达式todo_match_obj
是:
r"""(?:/{0,2}\**\s?todo):?\s*(?P<todo>.+)"""
与我的 ipython shell 的快速对话给了我:
In [35]: print "//\todo"
// odo
In [36]: print r"""//\todo"""
//\todo
而且,以防万一 doctest 实现使用标准输出(我没有检查,抱歉):
In [37]: sys.stdout.write( r"""//\todo""" )
//\todo
从任何标准来看,我的 regex-foo 都不高,我意识到我可能在这里遗漏了一些东西。
编辑:在 Alex Martellis 回答之后,我想就什么正则表达式实际匹配 blasted 的建议提出建议r"""//\todo fix me"""
。我知道我最初并没有要求别人做我的作业,我会接受亚历克斯的回答,因为它确实回答了我的问题(或证实了我的恐惧)。但我保证在这里为我的问题投票任何好的解决方案:)
编辑编辑:作为参考,已在 kodos 项目中提交了一个错误:错误 #437633
我正在使用 Python 2.6.4(r264:75706,2009 年 12 月 7 日,18:45:15)
谢谢你读到这里(如果你直接跳过这里,我明白)