我的数据库中有一串字符串,如下所示:
下班开车回家。那只狗从沙发上跳了起来,在门口使他的主人强大起来。他把脸舔干净了。
字符串从句子的中间开始。我想要一个方法来切断最初不完整的句子,从“狗从沙发上跳到门口的主人。他舔干净了他的脸。”
我该怎么做?
发布于 2012-09-18 07:36:39
问题是如何定义不完整的句子。我们可以假设所有以大写字母开头的句子都是完整的句子。如果是这样,代码可能如下所示
str = 'driving home from work. The dog leaped of the sofa to great his master at the door. He licked his face clean.'
sentences = str.split('.')
sentences.shift if sentences[0][0].downcase == sentences[0][0]
sentences.join('.').strip << '.'有点棘手但很管用。
发布于 2012-09-18 07:43:39
最简单的答案是:
str = 'driving home from work. The dog leaped of the sofa to great his master at the door. He licked his face clean.'
str.sub!(/^[^A-Z].+?\./, '').strip!发布于 2012-09-18 07:42:02
https://stackoverflow.com/questions/12472212
复制相似问题