# Help replacing all @user patterns with link (regular expressions)

**URL:** <https://www.ruby-forum.com/t/help-replacing-all-user-patterns-with-link-regular-expressions/164859>\
**Category:** Rails\
**Created:** [April 6, 2009, 7:02pm UTC](https://www.ruby-forum.com/t/help-replacing-all-user-patterns-with-link-regular-expressions/164859 "2009-04-06T19:02:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [April 6, 2009, 7:02pm UTC](https://www.ruby-forum.com/t/help-replacing-all-user-patterns-with-link-regular-expressions/164859/1 "2009-04-06T19:02:37Z")

</div>

Hi - I want to add a feature to my comment forum where anytime someone  
types in @username, I can grab the username, make sure it’s valid, and  
if so, replace it with a link to that user’s profile. So, I think I  
want something like:

comment = comment.gsub("/@[A-Za-z0-9]\*/", ?)

How do I grab the tailing string (after dropping the @) in this  
regular expression, and how do I create a link string in the  
controller (link\_to?) ?

Thanks for any help,  
Dino

---

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [April 6, 2009, 7:22pm UTC](https://www.ruby-forum.com/t/help-replacing-all-user-patterns-with-link-regular-expressions/164859/2 "2009-04-06T19:22:33Z")

</div>

On 6 Apr 2009, at 17:53, dino d. wrote:

> regular expression, and how do I create a link string in the  
> controller (link\_to?) ?

Probably easiest to use the block form of gsub - your block gets  
called for each match, your block should return the replacement text.

Fred
