I'd like to match (and replace with a custom replacement function) each block of consecutive lines that all start by foo
. This nearly works:
import re
s = """bar6387
bar63287
foo1234
foohelloworld
fooloremipsum
baz
bar
foo236
foo5382
bar
foo879"""
def f(m):
print(m)
s = re.sub('(foo.*\n)+', f, s)
print(s)
# <re.Match object; span=(17, 53), match='foo1234\nfoohelloworld\nfooloremipsum\n'>
# <re.Match object; span=(61, 76), match='foo236\nfoo5382\n'>
but it fails to recognize the last block, obviously because it is the last line and there is no \n
at the end.
Is there a cleaner way to match a block of one or multiple consecutive lines starting with same pattern foo
?