[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Ruby Regex

Sriram Varahan

5/6/2009 7:19:00 AM

Hello,


I have this string:

"d:\\home\\abc2.zip\\abc.zip\\abc.com"

I need to extract the contents up to the first occurrence of zip which
would be:

"d:\\home\\abc2.zip"

When i use a regex like: \(.+).zip\ it gives me the entire contents upto
the second zip.

"d:\\home\\abc2.zip\\abc.zip" which is not what I am looking for.

Any solution to this?

Thanks.
Sriram.
--
Posted via http://www.ruby-....

6 Answers

Robert Dober

5/6/2009 7:31:00 AM

0

On Wed, May 6, 2009 at 9:18 AM, Sriram Varahan <sriram.varahan@gmail.com> w=
rote:
> Hello,
>
>
> =A0I have this string:
>
> =A0 =A0"d:\\home\\abc2.zip\\abc.zip\\abc.com"
>
> =A0I need to extract the contents up to the first occurrence of zip which
> would be:
>
> =A0"d:\\home\\abc2.zip"
>
> When i use a regex like: \(.+).zip\ it gives me the entire contents upto
> the second zip.
Why are you so greedy Sriram ;)? Well it is not you it is the "+"
which is greedy, try the non greedy version "+?" it might just give
you a nice surprise.
R.

Sriram Varahan

5/6/2009 7:47:00 AM

0

Was definitely a nice surprise! Thanks Robert for your help :)
--
Posted via http://www.ruby-....

Robert Klemme

5/6/2009 11:03:00 AM

0

2009/5/6 Robert Dober <robert.dober@gmail.com>:
> On Wed, May 6, 2009 at 9:18 AM, Sriram Varahan <sriram.varahan@gmail.com>=
wrote:
>> Hello,
>>
>>
>> =A0I have this string:
>>
>> =A0 =A0"d:\\home\\abc2.zip\\abc.zip\\abc.com"
>>
>> =A0I need to extract the contents up to the first occurrence of zip whic=
h
>> would be:
>>
>> =A0"d:\\home\\abc2.zip"
>>
>> When i use a regex like: \(.+).zip\ it gives me the entire contents upto
>> the second zip.
> Why are you so greedy Sriram ;)? Well it is not you it is the "+"
> which is greedy, try the non greedy version "+?" it might just give
> you a nice surprise.

Why not use File.dirname?

irb(main):003:0> File.dirname "d:\\home\\abc2.zip\\abc.zip\\abc.com"
=3D> "d:\\home\\abc2.zip\\abc.zip"

Cheers

robert


--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestprac...

Srijayanth Sridhar

5/6/2009 11:07:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

He only wanted the first occurrence of zip I think.

Jayanth

On Wed, May 6, 2009 at 4:33 PM, Robert Klemme <shortcutter@googlemail.com>wrote:

> 2009/5/6 Robert Dober <robert.dober@gmail.com>:
> > On Wed, May 6, 2009 at 9:18 AM, Sriram Varahan <sriram.varahan@gmail.com>
> wrote:
> >> Hello,
> >>
> >>
> >> I have this string:
> >>
> >> "d:\\home\\abc2.zip\\abc.zip\\abc.com"
> >>
> >> I need to extract the contents up to the first occurrence of zip which
> >> would be:
> >>
> >> "d:\\home\\abc2.zip"
> >>
> >> When i use a regex like: \(.+).zip\ it gives me the entire contents upto
> >> the second zip.
> > Why are you so greedy Sriram ;)? Well it is not you it is the "+"
> > which is greedy, try the non greedy version "+?" it might just give
> > you a nice surprise.
>
> Why not use File.dirname?
>
> irb(main):003:0> File.dirname "d:\\home\\abc2.zip\\abc.zip\\abc.com"
> => "d:\\home\\abc2.zip\\abc.zip"
>
> Cheers
>
> robert
>
>
> --
> remember.guy do |as, often| as.you_can - without end
> http://blog.rubybestprac...
>
>

Robert Klemme

5/6/2009 11:54:00 AM

0

2009/5/6 Srijayanth Sridhar <srijayanth@gmail.com>:
> He only wanted the first occurrence of zip I think.

Ah, yes. I overlooked that. Sorry for the noise.

IMHO an anchor is in order:

irb(main):001:0> "d:\\home\\abc2.zip\\abc.zip\\abc.com"[/\A.*?\.zip/]
=> "d:\\home\\abc2.zip"

Cheers

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestprac...