Replace all text between 2 strings python

前端 未结 5 1447
再見小時候
再見小時候 2020-11-27 22:18

Lets say I have:

a = r\'\'\' Example
This is a very annoying string
that takes up multiple lines
and h@s a// kind{s} of stupid symbols in it
ok String\'\'\'
         


        
5条回答
  •  悲&欢浪女
    2020-11-27 22:42

    Another method is to use string splits:

    def replaceTextBetween(originalText, delimeterA, delimterB, replacementText):
        leadingText = originalText.split(delimeterA)[0]
        trailingText = originalText.split(delimterB)[1]
    
        return leadingText + delimeterA + replacementText + delimterB + trailingText
    

    Limitations:

    • Does not check if the delimiters exist
    • Assumes that there are no duplicate delimiters
    • Assumes that delimiters are in correct order

提交回复
热议问题