重新匹配模式但没有获得所需的输出

时间:2015-08-20 11:25:01

标签: python formatting

我试图让python脚本在每次到达带有大写字母的单词时断开并继续换行。到目前为止,模式是匹配的,但是由于某种原因,它会在某些时候停止在字符串中插入新行。我没有看到模式有任何变化,匹配列表显示表达式工作并捕获所有大写字母一直到最后。 这是我的代码:

SLF4J: The following loggers will not work because they were created
SLF4J: during the default configuration phase of the underlying logging system.
SLF4J: See also http://www.slf4j.org/codes.html#substituteLogger
SLF4J: org.apache.activemq.ActiveMQPrefetchPolicy
SLF4J: org.apache.activemq.jndi.ReadOnlyContext
SLF4J: org.apache.activemq.transport.tcp.TcpTransportFactory
SLF4J: org.apache.activemq.util.ServiceSupport
SLF4J: org.apache.activemq.transport.TransportSupport
SLF4J: org.apache.activemq.transport.tcp.TcpTransport
SLF4J: org.apache.activemq.transport.TransportLoggerFactory
SLF4J: org.apache.activemq.transport.TransportLoggerView
SLF4J: org.apache.activemq.transport.InactivityMonitor
SLF4J: org.apache.activemq.transport.WireFormatNegotiator
SLF4J: org.apache.activemq.transport.ResponseCorrelator
SLF4J: org.apache.activemq.ActiveMQConnection
SLF4J: org.apache.activemq.util.IdGenerator
SLF4J: org.apache.activemq.transport.FutureResponse
SLF4J: org.apache.activemq.AdvisoryConsumer
SLF4J: org.apache.activemq.ActiveMQSession
SLF4J: org.apache.activemq.TransactionContext
SLF4J: org.apache.activemq.ActiveMQSessionExecutor
SLF4J: org.apache.activemq.management.JMSEndpointStatsImpl

期望的输出应该是:

农庄;而且,令我同意的失望,她的表现比无比好。

我敢于期待。

她似乎过于喜欢

先生。

林,甚至对他的妹妹,她表现出了很多的感情。

他们当然非常关心她的安慰。

金银花不是荆棘的弯曲,而是忍受刺的金银花。

有等...

而不是:

农庄;而且,令我愉快的失望,她表现得无比好  我敢于期待。  她似乎非常喜欢  先生。  林顿甚至对他的妹妹她表现出了很多的感情。当然,他们都很关心她的安慰。这不是刺金银花的刺,而是忍受刺的金银花。有

正如我上面所说的那样,清单显示re.findall在“过度喜欢Mr.Linton”之后捕获所有单词

1 个答案:

答案 0 :(得分:1)

就我而言,re工具箱中使用率最低的工具是split。如果我理解你的问题,这将完美地解决它:

  

inputstr ='''农庄;而且,令我同意的失望,她   表现得比我敢想的要好得多。她好像差不多了   对于林顿先生甚至对他的妹妹的过分喜爱,她表现出了很多   感情。当然,他们都很关心她的安慰。它   不是金银花的荆棘,而是金银花   拥抱刺。有'''

import re

result = re.split(r'\s+(?=[A-Z])', inputstr)
print('\n'.join(result))

这导致:

Grange; and, to my agreeable disappointment, she behaved infinitely better than
I dared to expect.
She seemed almost over-fond of
Mr.
Linton and even to his sister she showed plenty of affection.
They were both very attentive to her comfort, certainly.
It was not the thorn bending to the honeysuckles, but the honeysuckles embracing the thorn.
There