Ruby Regex


#1

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.


#2

On Wed, May 6, 2009 at 9:18 AM, Sriram V.
removed_email_address@domain.invalid wrote:

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


#3

Was definitely a nice surprise! Thanks Robert for your help :slight_smile:


#4

2009/5/6 Robert D. removed_email_address@domain.invalid:

“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


#5

He only wanted the first occurrence of zip I think.

Jayanth


#6

2009/5/6 Srijayanth S. removed_email_address@domain.invalid:

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