regexp help - substring of a backreference

  • Thread starter Bryan Kennerley
  • Start date
B

Bryan Kennerley

I'm putting together a web forum and want to parse web addresses that
are part of messages into clickable links. This is my attempt, I'm not
claiming its perfect but it works:

text.gsub!(/http:?\/*(\w+[\w\-\.\/~\?%&=#;,\+:\@]+)/,
' <a href="http://\1" target="_blank">http://\1</a>')

The problem is that some web addresses are incredibly long, so I want
the link text to be limited to, say, 50 chars. i.e. the second \1 needs
to be \1[0..49] or something. I can't figure out any easy way of doing
it, any ideas?
 
B

Benoit Daloze

I'm putting together a web forum and want to parse web addresses that
are part of messages into clickable links. This is my attempt, I'm not
claiming its perfect but it works:

text.gsub!(/http:?\/*(\w+[\w\-\.\/~\?%&=3D#;,\+:\@]+)/,
=A0 =A0 =A0 =A0 =A0' <a href=3D"http://\1" target=3D"_blank">http://\1</a=
')

The problem is that some web addresses are incredibly long, so I want
the link text to be limited to, say, 50 chars. i.e. the second \1 needs
to be \1[0..49] or something. I can't figure out any easy way of doing
it, any ideas?

$ ri gsub!

Implementation from String
---------------------------------------------------------------------------=
---
str.gsub!(pattern, replacement) -> str or nil
str.gsub!(pattern) {|match| block } -> str or nil
str.gsub!(pattern) -> an_enumerator

---------------------------------------------------------------------------=
---

Performs the substitutions of String#gsub in place, returning
str, or nil if no substitutions were performed. If no block and
no replacement is given, an enumerator is returned instead.
--

text.gsub(/http:?\/*(\w+[\w\-\.\/~\?%&=3D#;,\+:\@]+)/) { %Q{<a
href=3D"http://\1" target=3D"_blank">http://#{$1[0...50]}</a>} }
=3D> "<a href=3D\"http://\u0001\"
target=3D\"_blank\">http://mydgfsjceeeefknuxqbkqkhdslkjdhxfilunhefilxqhleiu=
fn</a>"

I am using %Q{str}, as "str" because you already have quotes. You
should also use %r{regex} for you Regexp, as it contains '/'.

Benoit Daloze
 
B

Benoit Daloze

I'm putting together a web forum and want to parse web addresses that
are part of messages into clickable links. This is my attempt, I'm not
claiming its perfect but it works:

text.gsub!(/http:?\/*(\w+[\w\-\.\/~\?%&=3D#;,\+:\@]+)/,
=A0 =A0 =A0 =A0 =A0' <a href=3D"http://\1" target=3D"_blank">http://\1</= a>')

The problem is that some web addresses are incredibly long, so I want
the link text to be limited to, say, 50 chars. i.e. the second \1 needs
to be \1[0..49] or something. I can't figure out any easy way of doing
it, any ideas?

$ ri gsub!

Implementation from String
-------------------------------------------------------------------------= -----
str.gsub!(pattern, replacement) -> str or nil
str.gsub!(pattern) {|match| block } -> str or nil
str.gsub!(pattern) -> an_enumerator

-------------------------------------------------------------------------= -----

Performs the substitutions of String#gsub in place, returning
str, or nil if no substitutions were performed. If no block and
no replacement is given, an enumerator is returned instead.
tp://\1" target=3D"_blank">http://#{$1[0...50]} said:
I am using %Q{str}, as "str" because you already have quotes. You should =
also use %r{regex} for you Regexp, as it contains '/'.
Benoit Daloze

Missed the first '\1' :
text.gsub(/http:?\/*(\w+[\w\-\.\/~\?%&=3D#;,\+:\@]+)/) {|match| %Q{<a
href=3D"http://#{$1}" target=3D"_blank">http://#{$1[0...50]}</a>} }
 
D

David A. Black

Hi --

I'm putting together a web forum and want to parse web addresses that
are part of messages into clickable links. This is my attempt, I'm not
claiming its perfect but it works:

text.gsub!(/http:?\/*(\w+[\w\-\.\/~\?%&=#;,\+:\@]+)/,
' <a href="http://\1" target="_blank">http://\1</a>')

The problem is that some web addresses are incredibly long, so I want
the link text to be limited to, say, 50 chars. i.e. the second \1 needs
to be \1[0..49] or something. I can't figure out any easy way of doing
it, any ideas?

You can use the block form, plus the thread-local $ variables:

text = "a bunch of miscellaneous words"
text.gsub!(/a b(.+)/) { $1[0,10] }
puts text # unch of mi


David

--
David A. Black, Senior Developer, Cyrus Innovation Inc.

The Ruby training with Black/Brown/McAnally
Compleat Philadelphia, PA, October 1-2, 2010
Rubyist http://www.compleatrubyist.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,839
Latest member
MartinaBur

Latest Threads

Top