当括号内的文本跨越多行时,以下通过模式的出现拆分字符串的函数不起作用:
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
函数中错误地使用了标志。
最佳答案
re.M
用于多行字符串中的 anchor 。你要的是re.S
, 这使得 .
匹配换行符。
关于python - 正则表达式拆分为多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16308215/