1

The following function which splits a string by the occurrence of a pattern doesn't work when the text inside brackets spans multiple lines:

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""")

I don't know if the regex is wrong or if I'm using incorrectly the flag in the splitfunction.

2 Answers 2

2

re.M is for anchors in multi-line strings. What you want is re.S, which makes . match newlines.

Sign up to request clarification or add additional context in comments.

Comments

2

You should use re.DOTALL flag:

re.S
re.DOTALL

Make the '.' special character match any character at all, including a newline; without this flag, '.' will match anything except a newline.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.