我正在尝试拆分这句话
"Mr. Smith bought cheapsite.com for 1.5 million dollars, i.e. he paid a lot " \
"for it. Did he mind? Adam Jones Jr. thinks he didn't. In any case, this " \
"isn't true... Well, with a probability of .9 it isn't."
进入下面的列表。
Mr. Smith bought cheapsite.com for 1.5 million dollars, i.e. he paid a lot for it.
Did he mind?
Adam Jones Jr. thinks he didn't.
In any case, this isn't true...
Well, with a probability of .9 it isn't.
代码:
print re.findall('([A-Z]+[^.].*?[a-z.][.?!] )[^a-z]',text)
输出:
['Mr. Smith bought cheapsite.com for 1.5 million dollars, i.e. he paid
a lot for it. ', "Adam Jones Jr. thinks he didn't. "]
K gud,但它遗漏了一些,有没有办法告诉 Python,因为最后一个 [^a-z] 不属于我的组,请从那里继续搜索。
编辑:
这是通过@sputnick 提到的前瞻性正则表达式实现的。
print re.findall('([A-Z]+[^.].*?[a-z.][.?!] )(?=[^a-z])',text)
输出:
['Mr. Smith bought cheapsite.com for 1.5 million dollars, i.e. he paid
a lot for it. ', 'Did he mind? ', "Adam Jones Jr. thinks he didn't. "
, "In any case, this isn't true... "]
但是我们还需要最后一句。有什么想法吗?
请您参考如下方法:
试试这个:
print re.findall('([A-Z]+[^.].*?[a-z.][.?!] )(?=[^a-z])',text)
使用正向预测正则表达式技术,检查http://www.regular-expressions.info/lookaround.html
