知识卡片
原始字符串减少一层转义混淆
r前缀减少Python对反斜线的解释,常适合书写正则,但正则引擎仍会解释自己的转义。它不保证表达式正确,也不是只允许单引号;应按可读性统一格式,理解原始字符串结尾反斜线等限制。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- · 查看原书依据
What about raw strings ( r'' and r"")? All regular expressions should be single-quoted raw strings, like r'match[ ]this', even if they don’t contain backslashes. Docstrings should be triple-quoted raw strings, like r""" This function does NLP """. That way, if you ever do add backslashes to your doctests or regular expressions, they will do what you expect.4
参考来源
- 位置:《Natural Language Processing in Action, Second Edition》附录B《附录B:Python与正则表达式》(来源项目:a19c7933-4eda-4176-aebd-30fe7e1ad292;源文件:extracted/Natural Language Processing in Action, Second Edition/_epub-src/OEBPS/Text/appendix-b.html)
- 结论依据:摘录中关于原始字符串减少一层转义混淆的定义、比较或条件支撑卡片;原书表述不严谨的范围已在覆盖记录逐项限定,不把案例结果外推为普遍结论。
- 原始内容:What about raw strings ( r'' and r"")? All regular expressions should be single-quoted raw strings, like r'match[ ]this', even if they don’t contain backslashes. Docstrings should be triple-quoted raw strings, like r""" This function does NLP """. That way, if you ever do add backslashes to your doctests or regular expressions, they will do what you expect.4