Searching Stings with Arrays?

Hi, I've been having a little problem with this. most of my ruby is from
rails and both are shaky.

okay what I've got is a string (which is a browser user agent) and I
would like to search that string with a predefined array of browsers.
I've done this in php so I'm positve an easy solution exists in Ruby

this is what I got
@string = "mozilla/5.0 (macintosh; u; ppc mac os x; en)
applewebkit/418.9.1 (khtml, like gecko) safari/419.3"

array = ["shiira", "msie", "safari", "firefox", "netscape"]

so I'd like to search the string for any matches, it should stop at
safari and bob's sombodys uncle.

this is what I've been trying to do.
array.find {|b| b == @string}.to_s

this does't work I know, but I'm I in the right direction?

···

--
Posted via http://www.ruby-forum.com/.

Hi --

Hi, I've been having a little problem with this. most of my ruby is from
rails and both are shaky.

okay what I've got is a string (which is a browser user agent) and I
would like to search that string with a predefined array of browsers.
I've done this in php so I'm positve an easy solution exists in Ruby

this is what I got
@string = "mozilla/5.0 (macintosh; u; ppc mac os x; en)
applewebkit/418.9.1 (khtml, like gecko) safari/419.3"

array = ["shiira", "msie", "safari", "firefox", "netscape"]

so I'd like to search the string for any matches, it should stop at
safari and bob's sombodys uncle.

this is what I've been trying to do.
array.find {|b| b == @string}.to_s

this does't work I know, but I'm I in the right direction?

You could use:

   @string.include?(b)

or, if you want to be more careful about false positives (like, if
"msie" was a substring in some other browser's string), you could do:

   array.find {|b| @string[/#{b}/] }

which anchors to word boundaries. (All untested, so check for typos
and/or logic errors :slight_smile:

David

···

On Thu, 21 Dec 2006, Phil Cooperking wrote:

--
Q. What's a good holiday present for the serious Rails developer?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
    aka The Ruby book for Rails developers!
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

array.find {|b| @string =~ /#{b}/}

Iterates over each entry of the array, makes a regexp of it (the
/#{b}/ bit) and matches that against the string. (Ideally, you want
@string =~ Regexp.escape(b), but with the strings you have it comes to
the same thing.)

martin

···

On 12/21/06, Phil Cooperking <phil@basicmind.co.uk> wrote:

this is what I got
@string = "mozilla/5.0 (macintosh; u; ppc mac os x; en)
applewebkit/418.9.1 (khtml, like gecko) safari/419.3"

array = ["shiira", "msie", "safari", "firefox", "netscape"]

so I'd like to search the string for any matches, it should stop at
safari and bob's sombodys uncle.

You'll probably have 10 (better) answers for this by the time I've
posted, but here's mine:

a = "something is here"
b = [ 'x', 'y', 'g', 'q', 'z' ]
b.find { |c| Regexp.new( c ).match( a ) } => "g"

The regexp.new creates a new regex (surprise) like /c/, and then
searches for it in the string a. If it's not found, match returns nil,
which is false, so find continues. If it's found, match is an object,
which is true, so find exits with that item from the array.

this is what I've been trying to do.
array.find {|b| b == @string}.to_s
this does't work I know, but I'm I in the right direction?

The find is on the right track, but what you're saying is that the
array item must match the full string ... which, based on your problem
description, is incorrect.

jz

Phil Cooperking wrote:

Hi, I've been having a little problem with this. most of my ruby is from
rails and both are shaky.

okay what I've got is a string (which is a browser user agent) and I
would like to search that string with a predefined array of browsers.
I've done this in php so I'm positve an easy solution exists in Ruby

this is what I got
@string = "mozilla/5.0 (macintosh; u; ppc mac os x; en)
applewebkit/418.9.1 (khtml, like gecko) safari/419.3"

array = ["shiira", "msie", "safari", "firefox", "netscape"]

so I'd like to search the string for any matches, it should stop at
safari and bob's sombodys uncle.

this is what I've been trying to do.
array.find {|b| b == @string}.to_s

this does't work I know, but I'm I in the right direction?

This might get you started off on the right path:

    m, agent = *@string.match(/(shiira|msie|safari|firefox|netscape)/)
    p agent

outputs

    safari

If you need further clarification of this code, just let me know.

Tom Werner

You already have your answers it seems, so for completeness I'll just mention a recent ruby quiz:

  Ruby Quiz - DictionaryMatcher (#103)

It focused on larger word sets that you're looking at, but it might give you a different perspective on the problem.

···

On Wed, 20 Dec 2006 22:06:49 -0000, Phil Cooperking <phil@basicmind.co.uk> wrote:

Hi, I've been having a little problem with this. most of my ruby is from
rails and both are shaky.

okay what I've got is a string (which is a browser user agent) and I
would like to search that string with a predefined array of browsers.
I've done this in php so I'm positve an easy solution exists in Ruby

this is what I got
@string = "mozilla/5.0 (macintosh; u; ppc mac os x; en)
applewebkit/418.9.1 (khtml, like gecko) safari/419.3"

array = ["shiira", "msie", "safari", "firefox", "netscape"]

so I'd like to search the string for any matches, it should stop at
safari and bob's sombodys uncle.

this is what I've been trying to do.
array.find {|b| b == @string}.to_s

this does't work I know, but I'm I in the right direction?

--
Ross Bamford - rosco@roscopeco.remove.co.uk

Hi --

> Hi, I've been having a little problem with this. most of my ruby is from
> rails and both are shaky.
>
> okay what I've got is a string (which is a browser user agent) and I
> would like to search that string with a predefined array of browsers.
> I've done this in php so I'm positve an easy solution exists in Ruby
>
> this is what I got
> @string = "mozilla/5.0 (macintosh; u; ppc mac os x; en)
> applewebkit/418.9.1 (khtml, like gecko) safari/419.3"
>
> array = ["shiira", "msie", "safari", "firefox", "netscape"]
>
> so I'd like to search the string for any matches, it should stop at
> safari and bob's sombodys uncle.
>
> this is what I've been trying to do.
> array.find {|b| b == @string}.to_s
>
> this does't work I know, but I'm I in the right direction?

You could use:

   @string.include?(b)

or, if you want to be more careful about false positives (like, if
"msie" was a substring in some other browser's string), you could do:

   array.find {|b| @string[/#{b}/] }

which anchors to word boundaries. (All untested, so check for typos
and/or logic errors :slight_smile:

Reminds me of Knuth's great saying
Beware of this code, I have only proved it correct, I have never tried it!
You'll be allright :wink:

David

--
Q. What's a good holiday present for the serious Rails developer?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
    aka The Ruby book for Rails developers!
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

R

···

On 12/20/06, dblack@wobblini.net <dblack@wobblini.net> wrote:

On Thu, 21 Dec 2006, Phil Cooperking wrote:

--
"The real romance is out ahead and yet to come. The computer revolution
hasn't started yet. Don't be misled by the enormous flow of money into bad
defacto standards for unsophisticated buyers using poor adaptations of
incomplete ideas."

- Alan Kay

dblack@wobblini.net wrote:

Hi --

> Hi, I've been having a little problem with this. most of my ruby is from
> rails and both are shaky.
>
> okay what I've got is a string (which is a browser user agent) and I
> would like to search that string with a predefined array of browsers.
> I've done this in php so I'm positve an easy solution exists in Ruby
>
> this is what I got
> @string = "mozilla/5.0 (macintosh; u; ppc mac os x; en)
> applewebkit/418.9.1 (khtml, like gecko) safari/419.3"
>
> array = ["shiira", "msie", "safari", "firefox", "netscape"]
>
> so I'd like to search the string for any matches, it should stop at
> safari and bob's sombodys uncle.
>
> this is what I've been trying to do.
> array.find {|b| b == @string}.to_s
>
> this does't work I know, but I'm I in the right direction?

You could use:

   @string.include?(b)

or, if you want to be more careful about false positives (like, if
"msie" was a substring in some other browser's string), you could do:

   array.find {|b| @string[/#{b}/] }

which anchors to word boundaries.

Are you sure?

array.find{|s| @string[/\b#{s}\b/] }

···

On Thu, 21 Dec 2006, Phil Cooperking wrote:

Tom Werner wrote:

Phil Cooperking wrote:

This might get you started off on the right path:

    m, agent = *@string.match(/(shiira|msie|safari|firefox|netscape)/)
    p agent

outputs

    safari

If you need further clarification of this code, just let me know.

Tom Werner

can I tag a regex on to this to pull the version of the browser? I
understand I might have to to break this down as some useragents are
differently setup.

···

--
Posted via http://www.ruby-forum.com/\.

William James wrote:

···

dblack@wobblini.net wrote:
> Hi --
>
> On Thu, 21 Dec 2006, Phil Cooperking wrote:
>
> > Hi, I've been having a little problem with this. most of my ruby is from
> > rails and both are shaky.
> >
> > okay what I've got is a string (which is a browser user agent) and I
> > would like to search that string with a predefined array of browsers.
> > I've done this in php so I'm positve an easy solution exists in Ruby
> >
> > this is what I got
> > @string = "mozilla/5.0 (macintosh; u; ppc mac os x; en)
> > applewebkit/418.9.1 (khtml, like gecko) safari/419.3"
> >
> > array = ["shiira", "msie", "safari", "firefox", "netscape"]
> >
> > so I'd like to search the string for any matches, it should stop at
> > safari and bob's sombodys uncle.
> >
> > this is what I've been trying to do.
> > array.find {|b| b == @string}.to_s
> >
> > this does't work I know, but I'm I in the right direction?
>
> You could use:
>
> @string.include?(b)
>
> or, if you want to be more careful about false positives (like, if
> "msie" was a substring in some other browser's string), you could do:
>
> array.find {|b| @string[/#{b}/] }
>
> which anchors to word boundaries.

Are you sure?

array.find{|s| @string[/\b#{s}\b/] }

We don't need no stinkin' loops:

p @string.split(/\W/) & array

Hi --

···

On Thu, 21 Dec 2006, William James wrote:

dblack@wobblini.net wrote:

Hi --

On Thu, 21 Dec 2006, Phil Cooperking wrote:

Hi, I've been having a little problem with this. most of my ruby is from
rails and both are shaky.

okay what I've got is a string (which is a browser user agent) and I
would like to search that string with a predefined array of browsers.
I've done this in php so I'm positve an easy solution exists in Ruby

this is what I got
@string = "mozilla/5.0 (macintosh; u; ppc mac os x; en)
applewebkit/418.9.1 (khtml, like gecko) safari/419.3"

array = ["shiira", "msie", "safari", "firefox", "netscape"]

so I'd like to search the string for any matches, it should stop at
safari and bob's sombodys uncle.

this is what I've been trying to do.
array.find {|b| b == @string}.to_s

this does't work I know, but I'm I in the right direction?

You could use:

   @string.include?(b)

or, if you want to be more careful about false positives (like, if
"msie" was a substring in some other browser's string), you could do:

   array.find {|b| @string[/#{b}/] }

which anchors to word boundaries.

Are you sure?

array.find{|s| @string[/\b#{s}\b/] }

Thanks. I had this weird feeling something was wrong when I sent that
message.... I think the 'b' variable name fooled me :slight_smile:

David

--
Q. What's a good holiday present for the serious Rails developer?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
    aka The Ruby book for Rails developers!
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

Phil Cooperking wrote:

Tom Werner wrote:
> Phil Cooperking wrote:
>>
>>
> This might get you started off on the right path:
>
> m, agent = *@string.match(/(shiira|msie|safari|firefox|netscape)/)
> p agent
>
> outputs
>
> safari
>
> If you need further clarification of this code, just let me know.
>
> Tom Werner

can I tag a regex on to this to pull the version of the browser? I
understand I might have to to break this down as some useragents are
differently setup.

@string = "mozilla/5.0 (macintosh; u; ppc mac os x; en)
applewebkit/418.9.1 (khtml, like gecko) safari/419.3"

array = ["shiira", "msie", "safari", "firefox", "netscape"]

@string =~ %r{(#{ array.map{|s| Regexp.escape(s)}.join('|')})/(\S+)}
p $1,$2

browser,version = @string.match(
  %r{(#{ array.map{|s| Regexp.escape(s)}.join('|')})/(\S+)}).
  captures
p browser,version

Yeah, that's why I changed it to "s".

···

dbl...@wobblini.net wrote:

Hi --

On Thu, 21 Dec 2006, William James wrote:

> dblack@wobblini.net wrote:
>> Hi --
>>
>> On Thu, 21 Dec 2006, Phil Cooperking wrote:
>>
>>> Hi, I've been having a little problem with this. most of my ruby is from
>>> rails and both are shaky.
>>>
>>> okay what I've got is a string (which is a browser user agent) and I
>>> would like to search that string with a predefined array of browsers.
>>> I've done this in php so I'm positve an easy solution exists in Ruby
>>>
>>> this is what I got
>>> @string = "mozilla/5.0 (macintosh; u; ppc mac os x; en)
>>> applewebkit/418.9.1 (khtml, like gecko) safari/419.3"
>>>
>>> array = ["shiira", "msie", "safari", "firefox", "netscape"]
>>>
>>> so I'd like to search the string for any matches, it should stop at
>>> safari and bob's sombodys uncle.
>>>
>>> this is what I've been trying to do.
>>> array.find {|b| b == @string}.to_s
>>>
>>> this does't work I know, but I'm I in the right direction?
>>
>> You could use:
>>
>> @string.include?(b)
>>
>> or, if you want to be more careful about false positives (like, if
>> "msie" was a substring in some other browser's string), you could do:
>>
>> array.find {|b| @string[/#{b}/] }
>>
>> which anchors to word boundaries.
>
> Are you sure?
>
> array.find{|s| @string[/\b#{s}\b/] }

Thanks. I had this weird feeling something was wrong when I sent that
message.... I think the 'b' variable name fooled me :slight_smile:

Hi --

···

On Thu, 21 Dec 2006, William James wrote:

array.find{|s| @string[/\b#{s}\b/] }

We don't need no stinkin' loops:

p @string.split(/\W/) & array

To each his own :slight_smile: I like the loop -- though I'm not sure why I used
instead of just matching. I think I somehow had map on the brain.

David

--
Q. What's a good holiday present for the serious Rails developer?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
    aka The Ruby book for Rails developers!
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)