更新时间:2023-02-22 12:48:02
该如何解决? 我会的:
import re
mystr = "&marker1\nThe String that I want /\n&marker1\nAnother string that I want /\n"
found = re.findall(r"\&marker1\n(.*?)/\n", mystr)
print(found)
输出:
['The String that I want ', 'Another string that I want ']
请注意:
&
在re
模式中具有特殊含义.您需要对其进行转义(\&
).
匹配除换行符之外的所有内容search
,则findall
更适合选择
*?
是非贪婪的,在这种情况下.*
也可以工作,因为.
与换行符不匹配,但是在其他情况下,匹配结束可能会超出您的期望&
has special meaning in re
patterns, if you want literal & you need to escape it (\&
).
does match anything except newlinesfindall
is better suited choiced if you just want list of matched substrings, rather than search
*?
is non-greedy, in this case .*
would work too, because .
do not match newline, but in other cases you might ending matching more than you wish阅读模块re
文档讨论原始字符串的用法以及具有特殊含义的隐式字符列表.
Read module re
documentation for discussion of raw-string usage and implicit list of characters with special meaning.