Reputation: 19
I would like to delete a sentence that is after a hash found in a sentence. This process should happen on all lines that have pound signs, for example:
abcde#efg hijk
aaaabbbcc
ghij#kloa.bcd
It will look like this
abcde#
aaaabbbcc
ghij#
I made the code below with re.findall
, but when it finds an empty space, it does not delete the rest, look:
text = 'abcde#efg hijk \n\n ghij#kloa.bcd'
result=re.findall(r'#(\w+.\w+\s+)', text)
>>['efg hijk \n\n ']
Does anyone have any ideas?
Upvotes: 0
Views: 59
Reputation: 57446
I'd use
re.findall(r'^.*?(?:$|#)', text, re.M)
to match all of the substrings you want to keep and
re.findall(r'(?<=#).*$', text, re.M)
to match all of the substrings you want to reject.
Both use the MULTILINE
flag and end-of-line $
or #
characters as boundaries.
Use caution when there are multiple #
s in a line.
Upvotes: 1