Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Looking for a match

Reply
Thread Tools

Looking for a match

 
 
Ari Brown
Guest
Posts: n/a
 
      06-25-2007
Hey,
I'm looking to match a string to another string ANYWHERE in a new
file. For instance, my code looks like such:

if readlines[*] == chosen_one # If there's a match -
next # Boil some brains (try
again)
end

My goal is to do a search through each line in the file's line
(readlines[*]) for the chosen_one.

Will Ruby support the wildcard I used? And if not, what could I do to
fix it? BTW, this is apart of the ruby quiz if that helps.

aRi
-------------------------------------------|
Nietzsche is my copilot



 
Reply With Quote
 
 
 
 
yermej@gmail.com
Guest
Posts: n/a
 
      06-26-2007
On Jun 25, 5:44 pm, Ari Brown <a...@aribrown.com> wrote:
> Hey,
> I'm looking to match a string to another string ANYWHERE in a new
> file. For instance, my code looks like such:
>
> if readlines[*] == chosen_one # If there's a match -
> next # Boil some brains (try
> again)
> end
>
> My goal is to do a search through each line in the file's line
> (readlines[*]) for the chosen_one.
>
> Will Ruby support the wildcard I used? And if not, what could I do to
> fix it? BTW, this is apart of the ruby quiz if that helps.
>
> aRi


If you've read the lines in as elements of an array:

lines = the_file.readlines
lines.any? {|line| line.match chosen_one} # true if there's a match

If you've read in the lines as one big string:

lines = the_file.read
!lines.match(chosen_one).nil? # true if there's a match

 
Reply With Quote
 
 
 
 
Aaron Patterson
Guest
Posts: n/a
 
      06-26-2007
On Tue, Jun 26, 2007 at 07:44:14AM +0900, Ari Brown wrote:
> Hey,
> I'm looking to match a string to another string ANYWHERE in a new
> file. For instance, my code looks like such:
>
> if readlines[*] == chosen_one # If there's a match -
> next # Boil some brains (try
> again)
> end


You should check out Enumerable#any?. I think that is what you are
looking for. For example:

if readlines.any? { |line| line == chosen_one }
...
end

--
Aaron Patterson
http://tenderlovemaking.com/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
re.sub(): replace longest match instead of leftmost match? John Gordon Python 13 12-20-2011 02:58 AM
pat-match.lisp or extend-match.lisp in Python? ekzept Python 0 08-10-2007 06:08 PM
Match doesn't match Volkan Civelek Ruby 4 07-19-2006 07:44 AM
$match = true() for empty $match?? Victor XML 2 05-17-2004 10:43 AM
Java regex can't match lengthy match? hiwa Java 0 01-29-2004 10:09 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57