Building an array of matching strings.

L

Lloyd Zusman

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.
 
J

James Edward Gray II

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?

matches = text.scan(...)

Hope that helps.

James Edward Gray II
 
L

Logan Capaldo

------=_Part_17708_11356772.1132527976582
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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 =3D []
text.gsub(pat) {
|m|
matchingStrings << m
}

Is there a cleaner and elegant way to do this?

Thanks in advance.


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

------=_Part_17708_11356772.1132527976582--
 
E

Ezra Zygmuntowicz

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
(e-mail address removed)
509-577-7732
 
L

Lloyd Zusman

Ezra Zygmuntowicz said:
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.
 
T

TomRossi7

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top