我该如何提取出给定词序列第一次出现之前的所有字符(包括换行符)?例如,对于以下输入:
输入文本:
"shantaram is an amazing novel.
It is one of the best novels i have read.
the novel is written by gregory david roberts.
He is an australian"
我想从shantaram中提取文本到第一次出现在第二行的the。
输出必须为-
shantaram is an amazing novel.
It is one of the
我已经尝试了一整个上午。我可以编写提取到特定字符之前所有字符的表达式,但是在这里,如果我使用类似于下面的表达式:
re.search("shantaram[\s\S]*the", string)
它不能跨越换行符匹配。