# Ruby Regex

**URL:** <https://www.ruby-forum.com/t/ruby-regex/167270>\
**Category:** Ruby\
**Created:** [May 6, 2009, 9:18am UTC](https://www.ruby-forum.com/t/ruby-regex/167270 "2009-05-06T09:18:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sriram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@sriram](https://www.ruby-forum.com/u/sriram)\
**Post date:** [May 6, 2009, 9:18am UTC](https://www.ruby-forum.com/t/ruby-regex/167270/1 "2009-05-06T09:18:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sriram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@sriram](https://www.ruby-forum.com/u/sriram)\
**Post date:** [May 6, 2009, 9:34am UTC](https://www.ruby-forum.com/t/ruby-regex/167270/2 "2009-05-06T09:34:13Z")

</div>

On Wed, May 6, 2009 at 9:18 AM, Sriram V.  
[removed\_email\_address@domain.invalid](mailto: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.

---

<div class="post-metadata">

**Author:** ![sriram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@sriram](https://www.ruby-forum.com/u/sriram)\
**Post date:** [May 6, 2009, 9:46am UTC](https://www.ruby-forum.com/t/ruby-regex/167270/3 "2009-05-06T09:46:56Z")

</div>

Was definitely a nice surprise! Thanks Robert for your help 🙂

---

<div class="post-metadata">

**Author:** ![sriram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@sriram](https://www.ruby-forum.com/u/sriram)\
**Post date:** [May 6, 2009, 1:04pm UTC](https://www.ruby-forum.com/t/ruby-regex/167270/4 "2009-05-06T13:04:12Z")

</div>

2009/5/6 Robert D. [removed\_email\_address@domain.invalid](mailto: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

---

<div class="post-metadata">

**Author:** ![sriram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@sriram](https://www.ruby-forum.com/u/sriram)\
**Post date:** [May 6, 2009, 1:08pm UTC](https://www.ruby-forum.com/t/ruby-regex/167270/5 "2009-05-06T13:08:55Z")

</div>

He only wanted the first occurrence of zip I think.

Jayanth

---

<div class="post-metadata">

**Author:** ![sriram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@sriram](https://www.ruby-forum.com/u/sriram)\
**Post date:** [May 6, 2009, 1:54pm UTC](https://www.ruby-forum.com/t/ruby-regex/167270/6 "2009-05-06T13:54:40Z")

</div>

2009/5/6 Srijayanth S. [removed\_email\_address@domain.invalid](mailto: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
