使用 Python 的 string.replace 与 re.sub [英] Use Python's string.replace vs re.sub
本文介绍了使用 Python 的 string.replace 与 re.sub的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于 Python 2.5、2.6,我应该使用 string.replace
还是 re.sub
进行基本的文本替换?
For Python 2.5, 2.6, should I be using string.replace
or re.sub
for basic text replacements?
在 PHP 中,这是明确说明的,但我找不到 Python 的类似注释.
In PHP, this was explicitly stated but I can't find a similar note for Python.
推荐答案
只要你能凑合使用str.replace()
,你就应该使用它.它避免了正则表达式的所有陷阱(如转义),并且通常速度更快.
As long as you can make do with str.replace()
, you should use it. It avoids all the pitfalls of regular expressions (like escaping), and is generally faster.
这篇关于使用 Python 的 string.replace 与 re.sub的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文