パターンの出現によって文字列を分割する次の関数は、括弧内のテキストが複数行にまたがる場合は機能しません。
import re
def header(text):
authors = [i.strip() for i in re.split(r'\\and|\\thanks\{.*?\}', text, flags=re.M)]
names = filter(None,authors)
return '{} and {}'.format(', '.join(names[:-1]), names[-1])
print header(r"""John Bar \and Tom Foo\thanks{Testing if this works with
multiple lines} \and Sam Baz""")
正規表現が間違っているのか、関数でフラグを間違って使用しているのかわかりませんsplit
。