Ruby Regex

S

Sriram Varahan

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

Robert Dober

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

Robert Klemme

2009/5/6 Robert Dober said:
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/
 
S

Srijayanth Sridhar

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

He only wanted the first occurrence of zip I think.

Jayanth

 
R

Robert Klemme

2009/5/6 Srijayanth Sridhar said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top