我有这样一个字符串:
----------
FT Weekend
----------
Why do we run marathons?
Are marathons and cycling races about more than exercise? What does the
literature of endurance tell us about our thirst for self-imposed hardship?
我想删除从 ------------
到下一个 ------------
的部分。
我一直在使用re.sub
:
pattern =r"-+\n.+\n-+"
re.sub(pattern, '', thestring)
最佳答案
pattern =r"-+\n.+?\n-+"
re.sub(pattern, '', thestring,flags=re.DOTALL)
只需使用 DOTALL
标志。您的正则表达式的问题是默认情况下 。
与 \n
不匹配。因此您需要明确添加标志 DOTALL
使其匹配 \n
。
查看演示。
https://regex101.com/r/hR7tH4/24
或
pattern =r"-+\n[\s\S]+?\n-+"
re.sub(pattern, '', thestring)
如果你不想添加标志
关于python - 如何编写正则表达式以在 python 中使用 re.split,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31403194/