I want to remove all lines that include a b
in this multiline string:
aba\n
aaa\n
aba\n
aaa\n
aba[\n\n - optional]
Note the file is not necessarily terminated by a newline character, or may have extra line breaks at the end that I want to keep.
This is the expected output:
aaa\n
aaa[\n\n - as in the input file]
This is what I have tried:
import re
String = "aba\naaa\naba\naaa\naba"
print(String)
print(re.sub(".*b.*", "", String)) # this one leaves three empty lines
print(re.sub(".*b.*\n", "", String)) # this one misses the last line
print(re.sub("\n.*b.*", "", String)) # this one misses the first line
print(re.sub(".*b.*\n?", "", String)) # this one leaves an empty last line
print(re.sub("\n?.*b.*", "", String)) # this one leaves an empty first line
print(re.sub("\n?.*b.*\n?", "", String)) # this one joins the two remaining lines
I have also tried out flags=re.M
and various look-aheads and -behinds, but the main question seems to be: how can I remove either the first or the last occurrence of \n
in a matching string, depending on which on exists - but not both, if both do exist?