# Building an array of matching strings

**URL:** <https://www.ruby-forum.com/t/building-an-array-of-matching-strings/48464>\
**Category:** Ruby\
**Created:** [November 21, 2005, 12:00am UTC](https://www.ruby-forum.com/t/building-an-array-of-matching-strings/48464 "2005-11-21T00:00:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ljz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/74df32/32.png) [@ljz](https://www.ruby-forum.com/u/ljz)\
**Post date:** [November 21, 2005, 12:00am UTC](https://www.ruby-forum.com/t/building-an-array-of-matching-strings/48464/1 "2005-11-21T00:00:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ljz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/74df32/32.png) [@ljz](https://www.ruby-forum.com/u/ljz)\
**Post date:** [November 21, 2005, 12:08am UTC](https://www.ruby-forum.com/t/building-an-array-of-matching-strings/48464/2 "2005-11-21T00:08:34Z")

</div>

On Nov 20, 2005, at 5:00 PM, Lloyd Z. wrote:

> ```
> |m|
> matchingStrings << m
> 
> ```
> 
> }
> 
> Is there a cleaner and elegant way to do this?

matches = text.scan(…)

Hope that helps.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![ljz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/74df32/32.png) [@ljz](https://www.ruby-forum.com/u/ljz)\
**Post date:** [November 21, 2005, 12:08am UTC](https://www.ruby-forum.com/t/building-an-array-of-matching-strings/48464/3 "2005-11-21T00:08:34Z")

</div>

On 11/20/05, Lloyd Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> text.gsub(pat) {  
> Lloyd Z.  
> removed\_email\_address@domain.invalid  
> God bless you.

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

---

<div class="post-metadata">

**Author:** ![ljz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/74df32/32.png) [@ljz](https://www.ruby-forum.com/u/ljz)\
**Post date:** [November 21, 2005, 12:12am UTC](https://www.ruby-forum.com/t/building-an-array-of-matching-strings/48464/4 "2005-11-21T00:12:37Z")

</div>

On Nov 20, 2005, at 3:00 PM, Lloyd Z. wrote:

> ```
> |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 Z.  
WebMaster  
Yakima Herald-Republic Newspaper  
removed\_email\_address@domain.invalid  
509-577-7732

---

<div class="post-metadata">

**Author:** ![ljz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/74df32/32.png) [@ljz](https://www.ruby-forum.com/u/ljz)\
**Post date:** [November 21, 2005, 12:20am UTC](https://www.ruby-forum.com/t/building-an-array-of-matching-strings/48464/5 "2005-11-21T00:20:41Z")

</div>

Ezra Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> > matchingStrings = []
> 
> 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.

---

<div class="post-metadata">

**Author:** ![ljz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/74df32/32.png) [@ljz](https://www.ruby-forum.com/u/ljz)\
**Post date:** [November 21, 2005, 3:57am UTC](https://www.ruby-forum.com/t/building-an-array-of-matching-strings/48464/6 "2005-11-21T03:57:50Z")

</div>

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?
