[Wtr-general] Need Regular Expression help with a URL string

Alan Ark arkie at compli.com
Thu Jan 11 13:13:34 EST 2007


Hi Paul.

 

The following should work:

 

irb(main):001:0> web_url = 'http://foo/bar.html'

=> "http://foo/bar.html"

irb(main):002:0> web_url=~/(.*)\//

=> 0

irb(main):003:0> p $1

"http://foo"

nil

 

The regex (.*)\/

 

Mean to match everything you can to a slash.  Since the * operator is “greedy” it will grab everything it can up to that last slash.

The var $1 is a temp var created by the regex.  It should contain the value captured by the parens.

 

Thanks

-Alan

 

 

   _____  

From: wtr-general-bounces at rubyforge.org [mailto:wtr-general-bounces at rubyforge.org] On Behalf Of Paul Carvalho
Sent: Thursday, January 11, 2007 9:54 AM
To: wtr-general at rubyforge.org
Subject: [Wtr-general] Need Regular Expression help with a URL string

 

Okay, I give up.  I've worked on this for over an hour and I need more experienced help.  (Web searches and book references haven't helped either.)

I have a URL in a string and I'd like to replace part of it but first I need to find the part that I care about.  Here's an example of the string: 

web_url = 'HYPERLINK "http://foo/bar.html"http://foo/bar.html'

Now, what I am trying to do is find the *last* forward slash (/) in that string so that I can then replace everything after it.  I am having difficulty finding that last slash though. 

I am trying to use the   str[ regexp, int ]  function, but I can't seem figure it out.

Can anyone help me with this regexp?  Once I get it I'll figure out what's the best function for what I need to do.  Thanks. 

Paul C.



--
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.1.410 / Virus Database: 268.16.9/622 - Release Date: 1/10/2007


-- 
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.410 / Virus Database: 268.16.9/622 - Release Date: 1/10/2007
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/wtr-general/attachments/20070111/69a11e0c/attachment.html 


More information about the Wtr-general mailing list