你可以这样做:
>>> import re
>>> st='Paragraph 4-2 says. i am going home$ early- Yes.'
>>> [m.group(1) for m in re.finditer(r'(.*?[.$\-])(?:\s+|$)',st)]
['Paragraph 4-2 says.', 'i am going home$', 'early-', 'Yes.']
如果您根本不打算修改匹配组(使用条带或其他东西),您也可以使用具有相同正则表达式的 findall :
>>> re.findall(r'(.*?[.$\-])(?:\s+|$)',st)
['Paragraph 4-2 says.', 'i am going home$', 'early-', 'Yes.']
正则表达式在此处进行了解释,但总结如下:
(.*?[.$\-]) is the capture group containing:
.*? Any character (except newline) 0 to infinite times [lazy]
[.$\-] Character class matching .$- one time
(?:\s+|$) Non-capturing Group containing:
\s+ First alternate: Whitespace [\t \r\n\f] 1 to infinite times [greedy]
| or
$ Second alternate: end of string
根据您的字符串,(.*?[.$\-])(?:[ ]+|$)
如果您不想\r\n\f
与\s