# Regular Expressions

**URL:** <https://www.ruby-forum.com/t/regular-expressions/49101>\
**Category:** Rails\
**Created:** [November 29, 2005, 10:42pm UTC](https://www.ruby-forum.com/t/regular-expressions/49101 "2005-11-29T22:42:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mememe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/977dab/32.png) [@mememe](https://www.ruby-forum.com/u/mememe)\
**Post date:** [November 29, 2005, 10:42pm UTC](https://www.ruby-forum.com/t/regular-expressions/49101/1 "2005-11-29T22:42:13Z")

</div>

Does anyone know how can I replace all [http://example.com](http://example.com) to [http://example.com](http://example.com) in text?

---

<div class="post-metadata">

**Author:** ![mememe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/977dab/32.png) [@mememe](https://www.ruby-forum.com/u/mememe)\
**Post date:** [November 29, 2005, 11:42pm UTC](https://www.ruby-forum.com/t/regular-expressions/49101/2 "2005-11-29T23:42:52Z")

</div>

Dmytro,

I’m not a Ruby guru yet, so sorry that it’s in perl (maybe a Ruby master  
can  
deliver the syntax).

First make sure the initial url has it’s special characters escaped  
$escaped\_url = regex.escape($url);

search and replace the url with the full link. The $1 is a so called  
backreference to whats between the brackets “(” and “)”.

```
$html_anchor =~ s/($escaped_url)/<a href="$1">$1<\/a>/g

```

(As you can see the “” is placed as “\</a\>” the slash is a special  
character and therefore escaped with the ""

I’ve tried it and it works. There’s only one ‘but’. The url fed into the  
regex  
is not checked on valid syntax yet.

Hope this helps.

Regards,

Gerard.

On Tuesday 29 November 2005 22:33, Dmytro tried to type something like:

> Does anyone know how can I replace all [http://example.com](http://example.com) to [http://example.com](http://example.com) in text?
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
“Who cares if it doesn’t do anything? It was made with our new  
Triple-Iso-Bifurcated-Krypton-Gate-MOS process …”

My $Grtz =~ Gerard;  
~  
:wq!

---

<div class="post-metadata">

**Author:** ![mememe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/977dab/32.png) [@mememe](https://www.ruby-forum.com/u/mememe)\
**Post date:** [November 29, 2005, 11:50pm UTC](https://www.ruby-forum.com/t/regular-expressions/49101/3 "2005-11-29T23:50:58Z")

</div>

Assuming your text is in a string called ‘text’, you just need to  
perform some regexp-fu!

result = text.gsub(/http://\S+/, ‘\0’)

Cheers!

-DF

---

<div class="post-metadata">

**Author:** ![mememe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/977dab/32.png) [@mememe](https://www.ruby-forum.com/u/mememe)\
**Post date:** [November 29, 2005, 11:46pm UTC](https://www.ruby-forum.com/t/regular-expressions/49101/4 "2005-11-29T23:46:56Z")

</div>

On Nov 29, 2005, at 1:33 PM, Dmytro wrote:

> Does anyone know how can I replace all [http://example.com](http://example.com) to [http://example.com](http://example.com) in text?
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Sure try this:

text = “[http://example.com](http://example.com) what ever man\n another junk word http://  
[www.example2.com](http://www.example2.com)”  
text.gsub!(/(http://\w\*.\*\w+.\w+\b)/){|url| “#  
{url}”}  
p text  
#=\> “[http://example.com](http://example.com) what ever man  
\n another junk word [http://  
www.example2.com](http://www.example2.com)”

That will do a substitution on all url’s in your text. It deals with  
[http://example.com](http://example.com) and [http://www.example.com](http://www.example.com). You will have to tweak  
the regex to get more than one subdomain to work though.

Enjoy-

-Ezra Z.  
Yakima Herald-Republic  
WebMaster  
[http://yakimaherald.com](http://yakimaherald.com)  
509-577-7732  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![mememe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/977dab/32.png) [@mememe](https://www.ruby-forum.com/u/mememe)\
**Post date:** [November 29, 2005, 11:58pm UTC](https://www.ruby-forum.com/t/regular-expressions/49101/5 "2005-11-29T23:58:40Z")

</div>

me wrote:

> Does anyone know how can I replace all [http://example.com](http://example.com) to [http://example.com](http://example.com) in text?

Maybe you can try auto\_link helper  
[http://api.rubyonrails.com/classes/ActionView/Helpers/TextHelper.html#M000419](http://api.rubyonrails.com/classes/ActionView/Helpers/TextHelper.html#M000419)

---

<div class="post-metadata">

**Author:** ![mememe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/977dab/32.png) [@mememe](https://www.ruby-forum.com/u/mememe)\
**Post date:** [November 30, 2005, 12:55am UTC](https://www.ruby-forum.com/t/regular-expressions/49101/6 "2005-11-30T00:55:31Z")

</div>

On Tue, Nov 29, 2005 at 09:33:35PM +0000, Dmytro wrote:

> Does anyone know how can I replace all [http://example.com](http://example.com) to [http://example.com](http://example.com) in text?

sed ‘s%[http://example.com](http://example.com)%[http://example.com](http://example.com)%’ \< input \> output

Of course, you may be looking for something more general, in which case  
you  
can specify a more complex regular expression (one which matches  
whatever  
you’re actually looking for, such as a general URL) inside parentheses,  
and  
then replace with ‘\1’.

Ruby-wise, that should reduce to:

output = input.gsub(/(http://[a-zA-Z/.?;%]+)/, ‘\1’)

Caveat: The regular expression in there is _guaranteed_ to be  
incomplete; I  
just gave it as an example (which happens to match the example you gave,  
but  
not necessarily much more).

A more complete regexp can be found at  
google://valid+url+regular+expression/, but be careful: not all of the  
examples you might find are equally correct…

- Matt

–  
“[the average computer user] has been served so poorly that he expects  
his  
system to crash all the time, and we witness a massive worldwide  
distribution of bug-ridden software for which we should be deeply  
ashamed.”  
– Edsger Dijkstra
