Does anyone know how can I replace all http://example.com to <a href="http://example.com">http://example.com</a> in text?
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 "</a>" 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 to <a > href="http://example.com">http://example.com</a> in text? > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > 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!
On Nov 29, 2005, at 1:33 PM, Dmytro wrote:> Does anyone know how can I replace all http://example.com to <a > href="http://example.com">http://example.com</a> in text? > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >Sure try this: text = "http://example.com what ever man\n another junk word http:// www.example2.com" text.gsub!(/(http:\/\/\w*\.*\w+\.\w+\b)/){|url| "<a href=''#{url}''># {url}</a>"} p text #=> "<a href=''http://example.com''>http://example.com</a> what ever man \n another junk word <a href=''http://www.example2.com''>http:// www.example2.com</a>" That will do a substitution on all url''s in your text. It deals with http://example.com and http://www.example.com. You will have to tweak the regex to get more than one subdomain to work though. Enjoy- -Ezra Zygmuntowicz Yakima Herald-Republic WebMaster http://yakimaherald.com 509-577-7732 ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org
Assuming your text is in a string called ''text'', you just need to perform some regexp-fu! result = text.gsub(/http:\/\/\S+/, ''<a href="\0">\0</a>'') Cheers! -DF On 11/30/05, Dmytro <me-K5u54NxrwEriB9QmIjCX8w@public.gmane.org> wrote:> Does anyone know how can I replace all http://example.com to <a > href="http://example.com">http://example.com</a> in text? > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
me wrote:> Does anyone know how can I replace all http://example.com to <a > href="http://example.com">http://example.com</a> in text?Maybe you can try auto_link helper http://api.rubyonrails.com/classes/ActionView/Helpers/TextHelper.html#M000419 -- Posted via http://www.ruby-forum.com/.
On Tue, Nov 29, 2005 at 09:33:35PM +0000, Dmytro wrote:> Does anyone know how can I replace all http://example.com to <a > href="http://example.com">http://example.com</a> in text?sed ''s%http://example.com%<a href="http://example.com">http://example.com</a>%'' < 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 ''<a href="\1">\1</a>''. Ruby-wise, that should reduce to: output = input.gsub(/(http:\/\/[a-zA-Z\/.?;%]+)/, ''<a href="\1">\1</a>'') 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
Dmytro, I''ve been digging around some more and came up with the following script: #!/usr/bin/ruby text = "http://example.com/" ; p text rip = text.gsub(/(http:\/\/example.com\/)/, ''<a href="\1">\1</a>'') ; p rip rip = text.gsub(/(http:\/\/example.com\/)/, ''<a href=\''\1\''>\1</a>'') ; p rip Output: "http://example.com/" "<a href=\"http://example.com/\">http://example.com/</a>" "<a href=''http://example.com/''>http://example.com/</a>" For some reason I''m not able yet to get the replacement string working with double quotes without the preceding backslashes in the output Hopefully one of the bigger ruby individuals can sort that last one out. Regards, Gerard. On Wednesday 30 November 2005 11:41, Dmytro tried to type something like:> Thanks, Gerard > But it does''nt work in Ruby... In fact, I need just regular expression... > For example: text=text.gsub(/#(\d+)/), ''link'') > > G> Dmytro, > > G> I''m not a Ruby guru yet, so sorry that it''s in perl (maybe a Ruby master > can G> deliver the syntax). > > G> First make sure the initial url has it''s special characters escaped > G> $escaped_url = regex.escape($url); > > G> search and replace the url with the full link. The $1 is a so called > G> backreference to whats between the brackets "(" and ")". > > G> $html_anchor =~ s/($escaped_url)/<a href="$1">$1<\/a>/g > > G> (As you can see the "</a>" is placed as "<\/a>" the slash is a special > G> character and therefore escaped with the "\" > > G> I''ve tried it and it works. There''s only one ''but''. The url fed into the > regex G> is not checked on valid syntax yet. > > G> Hope this helps. > > G> Regards, > > G> Gerard. > > G> On Tuesday 29 November 2005 22:33, Dmytro tried to type something like: > >> Does anyone know how can I replace all http://example.com to <a > >> href="http://example.com">http://example.com</a> in text? > >> > >> _______________________________________________ > >> Rails mailing list > >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > >> 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!
Gerard- Use puts instead of p to display the result of the replacement. I think you will see that you are actually getting the results without the escaped quotes but using p does an inspect on the string which shows the \" as escaped. Cheers- -Ezra On Nov 30, 2005, at 7:09 AM, Gerard wrote:> Dmytro, > > I''ve been digging around some more and came up with the following > script: > > #!/usr/bin/ruby > text = "http://example.com/" ; p text > rip = text.gsub(/(http:\/\/example.com\/)/, ''<a href="\1">\1</ > a>'') ; p rip > rip = text.gsub(/(http:\/\/example.com\/)/, ''<a href=\''\1\''>\1</ > a>'') ; p rip > > Output: > "http://example.com/" > "<a href=\"http://example.com/\">http://example.com/</a>" > "<a href=''http://example.com/''>http://example.com/</a>" > > For some reason I''m not able yet to get the replacement string > working with > double quotes without the preceding backslashes in the output > > Hopefully one of the bigger ruby individuals can sort that last one > out. > > Regards, > > Gerard. > > On Wednesday 30 November 2005 11:41, Dmytro tried to type something > like: >> Thanks, Gerard >> But it does''nt work in Ruby... In fact, I need just regular >> expression... >> For example: text=text.gsub(/#(\d+)/), ''link'') >> >> G> Dmytro, >> >> G> I''m not a Ruby guru yet, so sorry that it''s in perl (maybe a >> Ruby master >> can G> deliver the syntax). >> >> G> First make sure the initial url has it''s special characters >> escaped >> G> $escaped_url = regex.escape($url); >> >> G> search and replace the url with the full link. The $1 is a so >> called >> G> backreference to whats between the brackets "(" and ")". >> >> G> $html_anchor =~ s/($escaped_url)/<a href="$1">$1<\/a>/g >> >> G> (As you can see the "</a>" is placed as "<\/a>" the slash is a >> special >> G> character and therefore escaped with the "\" >> >> G> I''ve tried it and it works. There''s only one ''but''. The url fed >> into the >> regex G> is not checked on valid syntax yet. >> >> G> Hope this helps. >> >> G> Regards, >> >> G> Gerard. >> >> G> On Tuesday 29 November 2005 22:33, Dmytro tried to type >> something like: >>>> Does anyone know how can I replace all http://example.com to <a >>>> href="http://example.com">http://example.com</a> in text? >>>> >>>> _______________________________________________ >>>> Rails mailing list >>>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>>> 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! > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-Ezra Zygmuntowicz Yakima Herald-Republic WebMaster http://yakimaherald.com 509-577-7732 ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org
Ezra, This ruby newby learned some more today, thanx for that! Regards, Gerard. On Wednesday 30 November 2005 20:03, Ezra Zygmuntowicz tried to type something like:> Gerard- > > Use puts instead of p to display the result of the replacement. I > think you will see that you are actually getting the results without > the escaped quotes but using p does an inspect on the string which > shows the \" as escaped. > > Cheers- > -Ezra > > On Nov 30, 2005, at 7:09 AM, Gerard wrote: > > Dmytro, > > > > I''ve been digging around some more and came up with the following > > script: > > > > #!/usr/bin/ruby > > text = "http://example.com/" ; p text > > rip = text.gsub(/(http:\/\/example.com\/)/, ''<a href="\1">\1</ > > a>'') ; p rip > > rip = text.gsub(/(http:\/\/example.com\/)/, ''<a href=\''\1\''>\1</ > > a>'') ; p rip > > > > Output: > > "http://example.com/" > > "<a href=\"http://example.com/\">http://example.com/</a>" > > "<a href=''http://example.com/''>http://example.com/</a>" > > > > For some reason I''m not able yet to get the replacement string > > working with > > double quotes without the preceding backslashes in the output > > > > Hopefully one of the bigger ruby individuals can sort that last one > > out. > > > > Regards, > > > > Gerard. > > > > On Wednesday 30 November 2005 11:41, Dmytro tried to type something > > > > like: > >> Thanks, Gerard > >> But it does''nt work in Ruby... In fact, I need just regular > >> expression... > >> For example: text=text.gsub(/#(\d+)/), ''link'') > >> > >> G> Dmytro, > >> > >> G> I''m not a Ruby guru yet, so sorry that it''s in perl (maybe a > >> Ruby master > >> can G> deliver the syntax). > >> > >> G> First make sure the initial url has it''s special characters > >> escaped > >> G> $escaped_url = regex.escape($url); > >> > >> G> search and replace the url with the full link. The $1 is a so > >> called > >> G> backreference to whats between the brackets "(" and ")". > >> > >> G> $html_anchor =~ s/($escaped_url)/<a href="$1">$1<\/a>/g > >> > >> G> (As you can see the "</a>" is placed as "<\/a>" the slash is a > >> special > >> G> character and therefore escaped with the "\" > >> > >> G> I''ve tried it and it works. There''s only one ''but''. The url fed > >> into the > >> regex G> is not checked on valid syntax yet. > >> > >> G> Hope this helps. > >> > >> G> Regards, > >> > >> G> Gerard. > >> > >> G> On Tuesday 29 November 2005 22:33, Dmytro tried to type > >> > >> something like: > >>>> Does anyone know how can I replace all http://example.com to <a > >>>> href="http://example.com">http://example.com</a> in text? > >>>> > >>>> _______________________________________________ > >>>> Rails mailing list > >>>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > >>>> 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! > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > -Ezra Zygmuntowicz > Yakima Herald-Republic > WebMaster > http://yakimaherald.com > 509-577-7732 > ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > 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!