I am trying to automate some document editing and wondering if there's a way to program this to simplify it? I need to remove large portions of text that are in the center of a user input that varies each time but is surrounded by constants. For example, the user input would be a copy/paste in this format:
"A: Test Name Here
B: Test Date Here
C: Long details that are unnecessary here.
D: Summary here"
I would like to cut out C all the way up to the start of D to get:
"A: Test Name Here
B: Test Date Here
D: Summary here"
I want to do something like delete ["C" : "D" -1] and save the rest of the text. In the actual documents "C" is a more specific unique word and wouldn't actually delete every letter c in the text.
Seems simple but I can't figure it out. I appreciate any help!
You could use re.sub
:
import re
s = """\
A: Test Name Here
B: Test Date Here
C: Long details that are unnecessary here.
D: Summary here"""
print(s)
print("---------")
print(re.sub("C:[\s\S]*?(?=D:)", "", s))
Output:
A: Test Name Here
B: Test Date Here
C: Long details that are unnecessary here.
D: Summary here
---------
A: Test Name Here
B: Test Date Here
D: Summary here
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments