Reputation: 4439
I have a string a
a="to be removed blah blah \r\nrow 2 blah blah\r\nrow 3 more blah blah \r\nto be removed more blah blah \r\nrow 5 final blah blah"
I want to do essentially the following
a=a.replace_all("to be removed...\r\n" ,"")
so that the output looks like this:
"row 2 blah blah\r\nrow 3 more blah blah\r\nrow5 final blah blah"
however, I only know how the lines starts, not the rest
Upvotes: 1
Views: 808
Reputation: 70732
Another approach without using a regular expression:
>>> a = "\r\n".join([i for i in a.splitlines() if not 'to be removed' in i])
Upvotes: 1
Reputation: 5292
Another try may be combination of search
and sub
function of re
module-
>>> a = "to be removed blah blah \r\nrow 2 blah blah\r\nrow 3 more blah blah \r\nto be removed more blah blah \r\nrow 5 final blah blah"
>>> r = re.sub(re.search(r'to be removed blah blah[\s]+', a,re.I).group(), '', a)
>>> print r
Upvotes: 0
Reputation: 49318
Use re.sub
and put a wildcard .
with repetition *
that does not greedily include ?
the \r\n
:
>>> import re
>>> a = "to be removed blah blah \r\nrow 2 blah blah\r\nrow 3 more blah blah \r\nto be removed more blah blah \r\nrow 5 final blah blah"
>>> a = re.sub(r'to be removed.*?\r\n', '', a)
>>> a
'row 2 blah blah\r\nrow 3 more blah blah \r\nrow 5 final blah blah'
Upvotes: 4