[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

compare to strings

Clint Pidlubny

4/4/2006 10:26:00 PM

Hello,

What is the best approach to searching a string for another string?

For instance, I have:

url1 = 'http://www.ur...
url2 = 'http://www.url.com...

If part of url1 is in url2, like above, I'd like to declare it a
match. I'm sure this happens using a regular expression, but my
experience is limited with them.

The other problem is that I'm not going to be looking for just one
url1, but I have an entire database table full of those to compare to
an entire database table of url2.

Any thoughts on approaching this problem are appreciated.

Thanks
Clint


3 Answers

dblack

4/5/2006 12:36:00 AM

0

Clint Pidlubny

4/5/2006 2:11:00 AM

0

On 4/4/06, dblack@wobblini.net <dblack@wobblini.net> wrote:
> Hi --
>
> On Wed, 5 Apr 2006, Clint Pidlubny wrote:
>
> > Hello,
> >
> > What is the best approach to searching a string for another string?
> >
> > For instance, I have:
> >
> > url1 = 'http://www.ur...
> > url2 = 'http://www.url.com...
> >
> > If part of url1 is in url2, like above, I'd like to declare it a
> > match. I'm sure this happens using a regular expression, but my
> > experience is limited with them.
> >
> > The other problem is that I'm not going to be looking for just one
> > url1, but I have an entire database table full of those to compare to
> > an entire database table of url2.
> >
> > Any thoughts on approaching this problem are appreciated.
>
> It's not a complete answer, but in case it helps: String has an
> include? method:
>
> url2.include?(url1) => true
>
>
> David
>
> --
> David A. Black (dblack@wobblini.net)
> Ruby Power and Light, LLC (http://www.rubypoweran...)
>
> "Ruby for Rails" chapters now available
> from Manning Early Access Program! http://www.manning.com/b...
>
>

I can't think of why that wouldn't work. Thank you.

Clint


Zach Dennis

4/5/2006 6:47:00 AM

0

dblack@wobblini.net wrote:
> Hi --
>
> On Wed, 5 Apr 2006, Clint Pidlubny wrote:
>
>> Hello,
>>
>> What is the best approach to searching a string for another string?
>>
>> For instance, I have:
>>
>> url1 = 'http://www.ur...
>> url2 = 'http://www.url.com...
>>
>> If part of url1 is in url2, like above, I'd like to declare it a
>> match. I'm sure this happens using a regular expression, but my
>> experience is limited with them.
>>
>> The other problem is that I'm not going to be looking for just one
>> url1, but I have an entire database table full of those to compare to
>> an entire database table of url2.
>>
>> Any thoughts on approaching this problem are appreciated.
>
> It's not a complete answer, but in case it helps: String has an
> include? method:
>
> url2.include?(url1) => true

Using String#include? is much faster then regexp matching. Here are some
benchmarks. I didn't test this with Oniguruma though, but I su

-- START CODE --
require 'benchmark'

url = "http://www.url....
url2 = "http://www.url.com/...

Benchmark.bm{ |x|
x.report{ 100000.times { url2.include?( url ) } }
x.report{ 100000.times { url2 =~ /#{url}/ } }
}
-- END CODE ---


Benchmark Windows ruby 1.8.4 (2005-12-24) [i386-mswin32]
C:\source\projects\ruby\strings>ruby temp.rb
user system total real
0.080000 0.000000 0.080000 ( 0.080000)
1.722000 0.130000 1.852000 ( 1.873000)


Benchmark Linux ruby 1.8.4 (2005-12-24) [i686-linux]
zdennis@lima:~$ ruby-1.8.4 temp.rb
user system total real
0.100000 0.000000 0.100000 ( 0.119403)
1.570000 0.040000 1.610000 ( 1.760446)


Benchmark Linux ruby 1.8.3 (2005-06-23) [i486-linux]
zdennis@lima:~$ ruby temp.rb
user system total real
0.160000 0.030000 0.190000 ( 0.209436)
1.720000 0.080000 1.800000 ( 2.021754)


Benchmark Linux ruby 1.8.2 (2005-04-11) [i386-linux]
zdennis@jboss:~$ ruby temp.rb
user system total real
0.000000 0.000000 0.000000 ( 0.246239)
0.000000 0.000000 0.000000 ( 1.401049)


Zach