From: Ezra Zygmuntowicz Date: 2005-11-21T08:10:09+09:00 Subject: Re: Building an array of matching strings. On Nov 20, 2005, at 3:00 PM, Lloyd Zusman wrote: > I want to search through a block of text and build an array of all > strings contained within this text that match a given pattern. I know > the following hack will work, but it seems like a misuse of the gsub > method: > > # Assume that 'text' contains the block of text and that 'pat' > # is a Regexp instance containing the pattern to match. > > matchingStrings = [] > text.gsub(pat) { > |m| > matchingStrings << m > } > > Is there a cleaner and elegant way to do this? > > Thanks in advance. > result_array = text.scan(/pat/) That will build an array in result_array of all the matches to /pat/ HTH- -Ezra Zygmuntowicz WebMaster Yakima Herald-Republic Newspaper ezra@yakima-herald.com 509-577-7732