我有一个文本块,其中有多个段落被不同长度的虚线分割。我想使用python匹配段落之间的行。我的要求如下:
下面是一个示例文本块:
Believing neglected so so allowance existence departure in.
In design active temper be uneasy. Thirty for remove plenty
regard you summer though. He preference connection astonished
on of yet. ------ Partiality on or continuing in particular principles as.
Do believing oh disposing to supported allowance we.
-------
Admiration we surrounded possession frequently he.
Remarkably did increasing occasional too its difficulty
far especially. Known tiled but sorry joy balls. Bed sudden
manner indeed fat now feebly. Face do with in need of
wife paid that be. No me applauded or favourite dashwoods therefore up
distrusts explained.
----t--
------
And produce say the ten moments parties. Simple innate summer
fat appear basket his desire joy. Outward clothes promise at gravity
do excited.
Sufficient particular impossible by reasonable oh expression is. Yet
preference
connection unpleasant yet melancholy but end appearance. And
excellence partiality
estimating terminated day everything.
---------
我试过以下方法:
r"-*.-"g or (.*?)-+
但是,我匹配所有包含两个或更多破折号的行,包括包含其他字符的行。你知道吗
r'^[^-]*$'
将匹配任何不包含
-
的行为了用
re.M
解析多行输入,还需要使用multiline标志在此处查看结果: https://regex101.com/r/iRkPep/1
只要
r"^[-]+$"
就行了。只需记住为^
和$
指定MULTILINE
模式,以便分别匹配行首和行尾,而不仅仅是整个字符串的开头和结尾。你知道吗实际上,最后一行不匹配,因为它的末尾有空格。如果允许在破折号后加空格,则可以使用
r"^[-]+[ ]*$"
。你知道吗另一件事-如果您还想只匹配段落之间的行,而不是在最后,您可以使用
r"^[-]+[ ]*$[^\Z]"
编辑:摘自@sln的评论,以下是一些我忘记的细微差别:
(?m)
来设置MULTILINE
标志[^\S\r\n]
匹配除换行符以外的所有空白。您可以使用它来代替[ ]
,后者只匹配空格。你知道吗相关问题 更多 >
编程相关推荐