This is probably one of those simple things that I am missing, but I have not been able to find a solution that would solve my issue.
I have two strings that are in the following format:
s1 = '87, 72 Start I am a sentence finish'
s2 = '93, 83 Start I am a sentence end'
Following this answer, Replace all text between 2 strings python, I am able to replace a phrase when given a start and end word, as the following.
import re
s1 = '87, 72 Start I am a sentence finish'
s2 = '93, 83 Start I am a sentence end'
print(re.sub("Start.*?finish", '', s1, re.DOTALL).strip())
print(re.sub("Start.*?end", '', s2, re.DOTALL).strip())
>>> 87, 72
>>> 93, 83
In my case, I will have conditions where the starting word is the same, but the ending word could be different.
Is it possible to replace the desired phrase by providing only the starting word?
I have tried this, but it only replaces the starting word.
s1 = '87, 72 Start I am a sentence finish'
print(re.sub("Start.*?", '', v1, re.DOTALL).strip())
>>> 87, 72 I am a sentence finish
Use an end of line anchor $
and greedy matching .*
:
print(re.sub("Start.*$", '', v1, re.DOTALL).strip())
See demo
Sample code:
import re
p = re.compile(ur'Start.*$')
test_str = u"87, 72 Start I am a sentence finish"
result = re.sub(p, "", test_str).strip()
print result
Output:
87, 72
You can use "$" to match the "end of line", so "Start.*$" should do it.
Also.. you can just remove ?
(non greedy) in your regex.. it will match till end by default.. (greedy and no need to use $
here)
print(re.sub("Start.*", '', v1, re.DOTALL).strip())
See DEMO
Input:
'87, 72 Start I am a sentence finish'
Output:
>>> 87, 72
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With