我试图将这句话分开
"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... "]
但我们仍然需要最后一句话。有什么想法吗?
答案 0 :(得分:2)
试试这个:
print re.findall('([A-Z]+[^.].*?[a-z.][.?!] )(?=[^a-z])',text)
使用正面预测正则表达式技术,检查http://www.regular-expressions.info/lookaround.html
答案 1 :(得分:1)
(.+?)(?<=(?<![A-Z][a-z])(?<![a-z]\.[a-z])(?:\.|\?)(?=\s|$))
试试这个。参见demo.Grab捕获或分组。使用re.findall
。
答案 2 :(得分:0)
最后
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... ", "Well, with a probability of .9
it isn't."]