Building an array of matching strings

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.

···

--
Lloyd Zusman
ljz@asfast.com
God bless you.

matches = text.scan(...)

Hope that helps.

James Edward Gray II

···

On Nov 20, 2005, at 5: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?

I think what you want is matchingStrings = text.scan(pat)

···

On 11/20/05, Lloyd Zusman <ljz@asfast.com> 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.

--
Lloyd Zusman
ljz@asfast.com
God bless you.

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

···

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.

Ezra Zygmuntowicz <ezra@yakima-herald.com> writes:

···

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/

Aha! I had forgotten about the 'scan' method! Thanks to all of you who
responded.

--
Lloyd Zusman
ljz@asfast.com
God bless you.

What if we wanted to expand it a little? For instance, Iets say the
person has a typo like phat instead of pat. Is there a good method for
searching and returning CLOSEST matches but not exact?