# Regexp help - substring of a backreference

**URL:** <https://www.ruby-forum.com/t/regexp-help-substring-of-a-backreference/192880>\
**Category:** Ruby\
**Created:** [August 7, 2010, 3:33pm UTC](https://www.ruby-forum.com/t/regexp-help-substring-of-a-backreference/192880 "2010-08-07T15:33:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![iamnotahorse](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/49beb7/32.png) [@iamnotahorse](https://www.ruby-forum.com/u/iamnotahorse)\
**Post date:** [August 7, 2010, 3:33pm UTC](https://www.ruby-forum.com/t/regexp-help-substring-of-a-backreference/192880/1 "2010-08-07T15:33:10Z")

</div>

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-./~?%&=#;,+:@]+)/,  
’ http://\1’)

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?

---

<div class="post-metadata">

**Author:** ![iamnotahorse](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/49beb7/32.png) [@iamnotahorse](https://www.ruby-forum.com/u/iamnotahorse)\
**Post date:** [August 7, 2010, 4:03pm UTC](https://www.ruby-forum.com/t/regexp-help-substring-of-a-backreference/192880/2 "2010-08-07T16:03:09Z")

</div>

On 7 August 2010 15:33, Bryan K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## it, any ideas?
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

$ 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-./~?%&=#;,+:@]+)/) { %Q{http://#{$1[0…50]}} }  
=\> “\<a href="http://\u0001"  
target="\_blank"\>[http://mydgfsjceeeefknuxqbkqkhdslkjdhxfilunhefilxqhleiufn](http://mydgfsjceeeefknuxqbkqkhdslkjdhxfilunhefilxqhleiufn)”

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 D.

---

<div class="post-metadata">

**Author:** ![iamnotahorse](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/49beb7/32.png) [@iamnotahorse](https://www.ruby-forum.com/u/iamnotahorse)\
**Post date:** [August 7, 2010, 4:04pm UTC](https://www.ruby-forum.com/t/regexp-help-substring-of-a-backreference/192880/3 "2010-08-07T16:04:54Z")

</div>

On 7 August 2010 16:02:31 UTC+2, Benoit D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > to be \1[0…49] or something. I can’t figure out any easy way of doing  
> > str.gsub!(pattern, replacement) → str or nil  
> > text.gsub(/http:?/\*(\w+[\w-./~?%&=#;,+:@]+)/) { %Q{http://#{$1[0…50]}} }
> 
> 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 D.

Missed the first ‘\1’ :  
text.gsub(/http:?/\*(\w+[\w-./~?%&=#;,+:@]+)/) {|match| %Q{http://#{$1[0…50]}} }

---

<div class="post-metadata">

**Author:** ![iamnotahorse](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/49beb7/32.png) [@iamnotahorse](https://www.ruby-forum.com/u/iamnotahorse)\
**Post date:** [August 7, 2010, 6:55pm UTC](https://www.ruby-forum.com/t/regexp-help-substring-of-a-backreference/192880/4 "2010-08-07T18:55:00Z")

</div>

Benoit D. wrote:

> text.gsub(/http:?/\*(\w+[\w-./~?%&=#;,+:@]+)/) {|match| %Q{http://#{$1[0…50]}} }

Thanks, works a charm 🙂

---

<div class="post-metadata">

**Author:** ![iamnotahorse](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/49beb7/32.png) [@iamnotahorse](https://www.ruby-forum.com/u/iamnotahorse)\
**Post date:** [August 7, 2010, 4:08pm UTC](https://www.ruby-forum.com/t/regexp-help-substring-of-a-backreference/192880/5 "2010-08-07T16:08:25Z")

</div>

Hi –

On Sat, 7 Aug 2010, Bryan K. wrote:

> 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](http://www.compleatrubyist.com)
