StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing the strings?

原创连接:

Python字符串替换

问题:

Python:如何将两字符串之间的内容替换掉?

I have this string(问题源码):

str = '''
// DO NOT REPLACE ME //
Anything might be here. Numbers letters, symbols, and other strings.
// DO NOT REPLACE ME EITHER //
'''

I want to replace whatever is between those two lines, but I do not want to replace those strings. How do I do this?

翻译:我想替换两字符串之间的内容,但不替换字符。如何实现?

解答:

解答源码:

>>> s = '''
// DO NOT REPLACE ME //
Anything might be here. Numbers letters, symbols, and other strings.
// DO NOT REPLACE ME EITHER //
'''
>>> print(s) // DO NOT REPLACE ME //
Anything might be here. Numbers letters, symbols, and other strings.
// DO NOT REPLACE ME EITHER //
>>> import re
>>> start = '// DO NOT REPLACE ME //'
>>> end = '// DO NOT REPLACE ME EITHER //'
>>> replacement = 'stuff'
>>> match = re.match(r'(.+%s\s*).+?(\s*%s.+)' % (start, end), s, re.DOTALL)
>>> match.groups()
('\n // DO NOT REPLACE ME //\n ', '\n // DO NOT REPLACE ME EITHER //\n ')
>>> new = match.group(1) + replacement + match.group(2)
>>> print(new) // DO NOT REPLACE ME //
stuff
// DO NOT REPLACE ME EITHER //

解答原文:May cause problems if start or end contain special regex characters. In this case they don't.

翻译:如果startend 值中包含正值表达式的字符,在执行时可能会出问题.

StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing the strings?的更多相关文章

  1. python 字符串替换

    字符串替换可以用内置的方法和正则表达式完成.1用字符串本身的replace方法: a = 'hello word'b = a.replace('word','python')print b 2用正则表 ...

  2. python字符串替换的2种有效方法

    python 字符串替换可以用2种方法实现:1是用字符串本身的方法.2用正则来替换字符串 下面用个例子来实验下:a = 'hello word'我把a字符串里的word替换为python1用字符串本身 ...

  3. python字符串替换的2种方法

    python 字符串替换可以用2种方法实现:1是用字符串本身的方法.2用正则来替换字符串 下面用个例子来实验下:a = 'hello word'把a字符串里的word替换为python 1.用字符串本 ...

  4. python 字符串替换功能 string.replace()可以用正则表达式,更优雅

    说起来不怕人笑话,我今天才发现,python 中的字符串替换操作,也就是 string.replace() 是可以用正则表达式的. 之前,我的代码写法如下,粗笨: 自从发现了正则表达式也生效后,代码变 ...

  5. python 字符串替换、正则查找替换

    import re if __name__ == "__main__": url = " \n deded<a href = "">这是第 ...

  6. python字符串替换之re.sub()

    re.sub(pattern, repl, string, count=0, flags=0) pattern可以是一个字符串也可以是一个正则,用于匹配要替换的字符,如果不写,字符串不做修改.\1 代 ...

  7. python字符串截取与替换的例子

    python字符串截取与替换的多种方法 时间:2016-03-12 20:08:14来源:网络 导读:python字符串截取与替换的多种方法,以冒号分隔的字符串的截取方法,python字符串替换方法, ...

  8. Python字符串内建处理函数

    #coding=utf-8 __author__ = 'Administrator' # 字符串处理函数 s1 = "study python string function , I lov ...

  9. python 字符串处理

    介绍字符串相关的:比较,截取,替换,长度,连接,反转,编码,格式化,查找,复制,大小写,分割等操作 什么是字符串 字符串 字符串或串(String)是由数字.字母.下划线组成的一串字符.一般记为 s= ...

随机推荐

  1. 札记:Java异常处理

    异常概述 程序在运行中总会面临一些"意外"情况,良好的代码需要对它们进行预防和处理.大致来说,这些意外情况分三类: 交互输入 用户以非预期的方式使用程序,比如非法输入,不正当的操作 ...

  2. 基于 SailingEase WinForm Framework 开发优秀的客户端应用程序(1:概述)

    本系统文章将详细阐述客户端应用程序的设计理念,实现方法. 本系列文章以  SailingEase WinForm Framework 为基础进行设计并实现,但其中的设计理念及方法,亦适用于任何类型的客 ...

  3. CSharpGL(38)带初始数据创建Vertex Buffer Object的情形汇总

    CSharpGL(38)带初始数据创建Vertex Buffer Object的情形汇总 开始 总的来说,OpenGL应用开发者会遇到为如下三种数据创建Vertex Buffer Object的情形: ...

  4. MyBatis在insert插入操作时返回主键ID的配置

    在使用MyBatis做持久层时,insert语句默认是不返回记录的主键值,而是返回插入的记录条数:如果业务层需要得到记录的主键时,可以通过Mapper.XML配置的方式来完成这个功能. 在 INSER ...

  5. C#设计模式-状态者模式

    一. 状态者(State)模式 每个对象都有其对应的状态,而每个状态又对应一些相应的行为,如果某个对象有多个状态时,那么就会对应很多的行为.那么对这些状态的判断和根据状态完成的行为,就会导致多重条件语 ...

  6. select,epoll,poll比较

    介绍和比较 http://www.cnblogs.com/maociping/p/5132583.html 比较 http://www.dataguru.cn/thread-336032-1-1.ht ...

  7. Git异常:fatal: V1.0 cannot be resolved to branch.

    GitHub实战系列汇总:http://www.cnblogs.com/dunitian/p/5038719.html ———————————————————————————————————————— ...

  8. Grunt基本使用-V1.0

    浅语:grunt中文网:http://www.gruntjs.net/ 第一步:Grunt 依赖 Node.js 所以在安装之前确保你安装了 Node.js.然后开始安装 Grunt. 实际上,安装的 ...

  9. 带搜索的ComboBox

    带搜索的ComboBox就是给ComboBox一个依赖属性的ItemSource,然后通过数据源中是否包含要查询的值,重新给ComboBox绑定数据源. public class EditComboB ...

  10. WebStrom与Github连接

    刚开始接触GitHub,之前都是用git命令来提交代码,很是麻烦~~~,现在通过WebStrom将项目提交到GitHub网站上(简单容易了很多).  第一:webstrom创建项目然后上传到githu ...