From: Robert Dober Date: 2009-07-15T19:08:19+09:00 Subject: Re: Search in string with regular expression On 7/15/09, Micke Micke wrote: > Hi all > > I'm trying to build a new string from a existing one. > > The original string is: my frog name is froggy > I will pull out only: frog froggy > > I have tried the following: > ---- > > str1 = 'my frog name is froggy' > str2 = str1[/frog|froggy/] Maybe str2 = str1.scan( /\bfrog\w*\B/ ).join(" ") that is if you want to match frog at the start of words only (\b) and assure charcters of your choice up to the end of the word? As \w might be a little bit too permissive, in 1.9 you might prefer str2 = str1.scan( /\bfrog[[:alpha:]]*\B/ ).join(" ") and all those nice variations ;) HTH Robert > > puts str2 > > --- > > This will print only the string: frog > > I can get the information with this code: > ---- > > str1 = 'my frog name is froggy' > str2 = str1[/frog] + " " + str1[/froggy/] > > puts str2 > ---- > > This will print the string: frog froggy > But i think it is possible to solve this in a better way, Any > suggestions ? > > > // Micke > -- > Posted via http://www.ruby-forum.com/. > > -- Toutes les grandes personnes ont d’abord été des enfants, mais peu d’entre elles s’en souviennent. All adults have been children first, but not many remember. [Antoine de Saint-Exupéry]