Ruby Regex

Discussion in 'Ruby' started by Sriram Varahan, May 6, 2009.

  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.
    --
    Posted via http://www.ruby-forum.com/.
    Sriram Varahan, May 6, 2009
    #1
    1. Advertising

  2. Sriram Varahan

    Robert Dober Guest

    On Wed, May 6, 2009 at 9:18 AM, Sriram Varahan <> 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.
    Robert Dober, May 6, 2009
    #2
    1. Advertising

  3. Sriram Varahan, May 6, 2009
    #3
  4. 2009/5/6 Robert Dober <>:
    > On Wed, May 6, 2009 at 9:18 AM, Sriram Varahan <>=

    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.rubybestpractices.com/
    Robert Klemme, May 6, 2009
    #4
  5. [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 <>wrote:

    > 2009/5/6 Robert Dober <>:
    > > On Wed, May 6, 2009 at 9:18 AM, Sriram Varahan <>

    > 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.rubybestpractices.com/
    >
    >
    Srijayanth Sridhar, May 6, 2009
    #5
  6. 2009/5/6 Srijayanth Sridhar <>:
    > 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.rubybestpractices.com/
    Robert Klemme, May 6, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmViQnVzaGVsbA==?=

    Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine?

    =?Utf-8?B?SmViQnVzaGVsbA==?=, Oct 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    700
    =?Utf-8?B?SmViQnVzaGVsbA==?=
    Oct 22, 2005
  2. Rick Venter

    perl regex to java regex

    Rick Venter, Oct 29, 2003, in forum: Java
    Replies:
    5
    Views:
    1,622
    Ant...
    Nov 6, 2003
  3. Replies:
    2
    Views:
    597
  4. Xah Lee
    Replies:
    1
    Views:
    942
    Ilias Lazaridis
    Sep 22, 2006
  5. Replies:
    3
    Views:
    760
    Reedick, Andrew
    Jul 1, 2008
Loading...

Share This Page