# Regular expressions (extracting urls)

**URL:** <https://www.ruby-forum.com/t/regular-expressions-extracting-urls/84545>\
**Category:** Ruby\
**Created:** [February 5, 2007, 9:37pm UTC](https://www.ruby-forum.com/t/regular-expressions-extracting-urls/84545 "2007-02-05T21:37:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ntk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/48db29/32.png) [@ntk](https://www.ruby-forum.com/u/ntk)\
**Post date:** [February 5, 2007, 9:37pm UTC](https://www.ruby-forum.com/t/regular-expressions-extracting-urls/84545/1 "2007-02-05T21:37:43Z")

</div>

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, ‘\<a  
href=’\0’\>\0’)

link becomes:  
link = “Go here: http://www.something.com!”

Now…

When the link is this:  
link = “Go here: 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, ‘\<a  
href=’\1’\>\1’)

but how can I exclude links that start with href=" and href=’ ?  
The problem is that I don’t know how to specify that HREF cannot  
preceede the link (cannot write [^href], [^(href)] doesn’t seem to work  
either and it also screws \n … )

Please help !

David

---

<div class="post-metadata">

**Author:** ![ntk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/48db29/32.png) [@ntk](https://www.ruby-forum.com/u/ntk)\
**Post date:** [February 5, 2007, 11:11pm UTC](https://www.ruby-forum.com/t/regular-expressions-extracting-urls/84545/2 "2007-02-05T23:11:27Z")

</div>

On 05.02.2007 21:37, David K. wrote:

> href=’\0’\>\0’)
> 
> either and it also screws \n … )  
> Maybe it’s enough to do

s.gsub( /([^"’])\b(http[^\s"’]+)\b([^"’])/,  
‘\1\2\3’ )

That depends on your input text. This piece has some weaknesses, e.g.  
won’t substitute hrefs at the beginning and end of the string (you could  
pad with a whitespace).

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![ntk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/48db29/32.png) [@ntk](https://www.ruby-forum.com/u/ntk)\
**Post date:** [February 7, 2007, 5:20pm UTC](https://www.ruby-forum.com/t/regular-expressions-extracting-urls/84545/3 "2007-02-07T17:20:01Z")

</div>

Thank you… but yes… the problem is that it has to detect links at the  
beginning and the end. and also it has to check for HREF, because  
sometimes you can have the quotation marks before the link, even if it’s  
not precedeed by href.

Is there a better solution? thank you

Robert K. wrote:

> On 05.02.2007 21:37, David K. wrote:
> 
> > href=’\0’\>\0’)
> > 
> > either and it also screws \n … )  
> > Maybe it’s enough to do
> 
> s.gsub( /([^"’])\b(http[^\s"’]+)\b([^"’])/,  
> ‘\1\2\3’ )
> 
> That depends on your input text. This piece has some weaknesses, e.g.  
> won’t substitute hrefs at the beginning and end of the string (you could  
> pad with a whitespace).
> 
> Kind regards
> 
> robert

---

<div class="post-metadata">

**Author:** ![ntk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/48db29/32.png) [@ntk](https://www.ruby-forum.com/u/ntk)\
**Post date:** [February 7, 2007, 6:46pm UTC](https://www.ruby-forum.com/t/regular-expressions-extracting-urls/84545/4 "2007-02-07T18:46:14Z")

</div>

On 5 feb, 17:37, David K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> href='\0'\>\0')
> 
> 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, ‘\<a  
> 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.

---

<div class="post-metadata">

**Author:** ![ntk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/48db29/32.png) [@ntk](https://www.ruby-forum.com/u/ntk)\
**Post date:** [February 7, 2007, 8:05pm UTC](https://www.ruby-forum.com/t/regular-expressions-extracting-urls/84545/5 "2007-02-07T20:05:24Z")

</div>

On 07.02.2007 18:40, gga wrote:

> > but how can I exclude links that start with href=" and href=’ ?
> 
> You can exclude full words if you write regexes such as:
> 
> /(?!href=[’"])/

I’m afraid this won’t work: this is negative lookahead but what you  
really need here is negative look_behind_. That’s only possible with  
the new regex engine in 1.9.

Kind regards

robert
