您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python string.replace正则表达式

Python string.replace正则表达式

str.replace()v2 |v3无法识别正则表达式。

要使用正则表达式执行替换,请使用re.sub()v2 |v3

例如:

import re

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

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

import re

regex = re.compile(r"^.*interfaceOpDataFile.*$", re.IGNORECASE)
for line in some_file:
    line = regex.sub("interfaceOpDataFile %s" % fileIn, line)
    # do something with the updated line
python 2022/1/1 18:43:12 有282人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶