匹配不同长度的刺

2024-04-20 13:43:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个文本块,其中有多个段落被不同长度的虚线分割。我想使用python匹配段落之间的行。我的要求如下:

  1. 只包含不同长度虚线的匹配线
  2. 不包括包含破折号和任何其他字符的行

下面是一个示例文本块:

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 (.*?)-+

但是,我匹配所有包含两个或更多破折号的行,包括包含其他字符的行。你知道吗


Tags: orofin文本soonbeconnection
2条回答

r'^[^-]*$'

将匹配任何不包含-的行

为了用re.M解析多行输入,还需要使用multiline标志

在此处查看结果: https://regex101.com/r/iRkPep/1

只要r"^[-]+$"就行了。只需记住为^$指定MULTILINE模式,以便分别匹配行首和行尾,而不仅仅是整个字符串的开头和结尾。你知道吗

实际上,最后一行不匹配,因为它的末尾有空格。如果允许在破折号后加空格,则可以使用r"^[-]+[ ]*$"。你知道吗

另一件事-如果您还想只匹配段落之间的行,而不是在最后,您可以使用r"^[-]+[ ]*$[^\Z]"

编辑:摘自@sln的评论,以下是一些我忘记的细微差别:

  1. 您可以通过在模式的开头使用(?m)来设置MULTILINE标志
  2. 字符类[^\S\r\n]匹配除换行符以外的所有空白。您可以使用它来代替[ ],后者只匹配空格。你知道吗

相关问题 更多 >