Python string.replace正则表达式

Python string.replace正则表达式,第1张

Python string.replace正则表达式

str.replace()

v2 |
v3无法识别正则表达式。

要使用正则表达式执行替换,请使用

re.sub()

v2 |
v3。

例如:

import reline = re.sub(r"(?i)^.*interfaceOpDataFile.*$", "interfaceOpDataFile %s" % fileIn, line       )

循环中,最好先编译正则表达式:

import reregex = re.compile(r"^.*interfaceOpDataFile.*$", re.IGNORECASE)for line in some_file:    line = regex.sub("interfaceOpDataFile %s" % fileIn, line)    # do something with the updated line


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5646385.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存