From: gga Date: 2007-02-08T02:45:06+09:00 Subject: Re: Regular expressions (extracting urls) On 5 feb, 17:37, David Krmpotic wrote: > Hi! > > I have to extract an url from the text and make it a link (a href...).. > The trick is that I have to be careful not to replace the url, that are > already a part of the link. > > so: > > link = "Go here:http://www.something.com!" > link.gsub!(/https?:\/\/[a-z0-9\.\-\_=&\+\/\?]+/i, ' href=\'\0\'>\0') > > link becomes: > link = "Go here: href='http://www.something.com'>http://www.something.com!" > > Now... > > When the link is this: > link = "Go here: href='http://www.something.com'>http://www.something.com!" > > The regular expression must not replace it! > > I know that for example if I want to exclude the links that start with > xhttp, I can write: > link.gsub!(/([^x]https?:\/\/[a-z0-9\.\-\_=&\+\/\?]+)/i, ' href=\'\1\'>\1') > > but how can I exclude links that start with href=" and href=' ? You can exclude full words if you write regexes such as: /(?!href=['"])/ Be careful about greediness, thou. If you are doing any web scraping, you also should look into something like WWW::Mechanize, instead of re-inventing the wheel.