import re
import sys
text = sys.stdin.read()
# using str.find()
result = text[:text.find('\n\n') + 1]
# using re
result2 = re.match(r'(.*?)$^$', text, flags=re.DOTALL | re.MULTILINE).group(1)
# check that the result is the same
for r in [result, result2]:
print(repr(r))
assert result == result2
在这种情况下,您不需要正则表达式:
Output
如果以文本模式从文件读取输入,则Python会自动将特定于平台的换行符转换为'\n'。你知道吗
相关问题 更多 >
编程相关推荐