Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace all text between 2 strings python

Tags:

python

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'''

I need a way to do a replace(or just delete) and text in between "This" and "ok" so that when I call it, a now equals:

a = "Example String"

I can't find any wildcards that seem to work. Any help is much appreciated.

like image 354
cashman04 Avatar asked Apr 23 '13 02:04

cashman04


People also ask

How do you replace all letters in a string in Python?

A character in Python is also a string. So, we can use the replace() method to replace multiple characters in a string. It replaced all the occurrences of, Character 's' with 'X'.

How do you replace words in Python?

Python String replace() Method The replace() method replaces a specified phrase with another specified phrase. Note: All occurrences of the specified phrase will be replaced, if nothing else is specified.

How does re sub work in Python?

sub() function belongs to the Regular Expressions ( re ) module in Python. It returns a string where all matching occurrences of the specified pattern are replaced by the replace string. To use this function, we need to import the re module first.

Is there a replace all function in Python?

What is the string replace() method in Python? The replace() method is a built-in functionality offered in Python programming. It replaces all the occurrences of the old substring with the new substring. Replace() returns a new string in which old substring is replaced with the new substring.

How to replace multiple substrings in a string using regex in Python?

We will use two functions of the regex module- re.sub () and re.subn () to replace multiple substrings in a string. sub () - It replaces the contents of a string based on patterns. It takes a pattern or a string as the first argument.

How to replace text between two characters?

Use re.sub: It replaces the text between two charactersor symbolsor stringswith desired characteror symbolor string. format: re.sub('A?(.*?)B', P, Q, flags=re.DOTALL)

How do you replace all occurrences of a character in Python?

Replace all occurrences of a character in a string To replace all the occurrences of a character with a new character, we simply have to pass the old character and the new character as input to the replace() method when it is invoked on the string. You can observe this in the following example.

How to solve the string replacement problem in Python?

This problem can be solved using the nested replace method, which internally would create a temp. variable to hold the intermediate replacement state. The original string is : abbabba The string after replacement of positions : baabaab


5 Answers

You need Regular Expression:

>>> import re
>>> re.sub('\nThis.*?ok','',a, flags=re.DOTALL)
' Example String'
like image 82
Kabie Avatar answered Oct 17 '22 01:10

Kabie


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
like image 39
Zachary Canann Avatar answered Oct 17 '22 01:10

Zachary Canann


The DOTALL flag is the key. Ordinarily, the '.' character doesn't match newlines, so you don't match across lines in a string. If you set the DOTALL flag, re will match '.*' across as many lines as it needs to.

like image 36
faraday703 Avatar answered Oct 17 '22 01:10

faraday703


a=re.sub('This.*ok','',a,flags=re.DOTALL)
like image 39
Vaughn Cato Avatar answered Oct 16 '22 23:10

Vaughn Cato


Use re.sub : It replaces the text between two characters or symbols or strings with desired character or symbol or string.

format: re.sub('A?(.*?)B', P, Q, flags=re.DOTALL)
where 
A : character or symbol or string
B : character or symbol or string
P : character or symbol or string which replaces the text between A and B
Q : input string
re.DOTALL : to match across all lines
import re
re.sub('\nThis?(.*?)ok', '', a,  flags=re.DOTALL)

output : ' Example String'

Lets see an example with html code as input

input_string = '''<body> <h1>Heading</h1> <p>Paragraph</p><b>bold text</b></body>'''

Target : remove <p> tag

re.sub('<p>?(.*?)</p>', '', input_string,  flags=re.DOTALL)

output : '<body> <h1>Heading</h1> <b>bold text</b></body>'

Target : replace <p> tag with word : test

re.sub('<p>?(.*?)</p>', 'test', input_string,  flags=re.DOTALL)

otput : '<body> <h1>Heading</h1> test<b>bold text</b></body>'
like image 23
Govinda Avatar answered Oct 17 '22 01:10

Govinda