From: Caike Date: 2008-07-04T09:11:52+09:00 Subject: Re: Regular expressions and long text On Jun 25, 2:25 am, Raveendran Jazzez wrote: > Hi, > > I think u expect this output.. so pls try it.. > > str="Ruby is great. We all know that." > a= str.split('.').join(' ') > words=[] > words=a.scan(/\w+/) > > => words=["Ruby","is","great","We","all","know","that"] > > Regards, > P.Raveendranhttp://raveendran.wordpress.com > > > > unknown wrote: > > Hello guys, > > > I've started with Ruby a month ago and I am doing some works with > > strings > > and regular expressions. I am trying to take a long text and store the > > individual sentences in an array. I can split a sentence in words and > > store them in an array, but I cannot manage to do it with sentences. > > > I have used the following assignment to work with the words: > > > str = "Ruby is great" > > words = [] > > words = str.scan(/\w+/) > > > The result is words[0]="Ruby" words[1]="is" and words[3]="great" > > > I would like to do the following: > > > str = "Ruby is great. We all know that." > > > and get words[0]="Ruby is great" and ruby[1]="We all know that" > > > Any ideas on how to do it with a regular expression instead of looping > > through the string looking for the "."? > > > Thanks, > > > Guillermo > > -- > Posted viahttp://www.ruby-forum.com/. If you want to stick to a regex based solution. >> str = "one one one. two. three." => "one one one. two. three." >> str.scan(/\w[\s|\w]*./) => ["one one one.", "two.", "three."] And you could keep going adding more words in the same pattern >> str = "one one one. two. three. four. five." => "one one one. two. three. four. five." >> str.scan(/\w[\s|\w]*./) => ["one one one.", "two.", "three.", "four.", "five."] It may not be the best solution to this problem, but it is always good have your regexp skills up to date ;)