# Gsub question, not a regex question...including part of the

**URL:** <https://www.ruby-forum.com/t/gsub-question-not-a-regex-question-including-part-of-the/113896>\
**Category:** Rails\
**Created:** [September 5, 2007, 11:53pm UTC](https://www.ruby-forum.com/t/gsub-question-not-a-regex-question-including-part-of-the/113896 "2007-09-05T23:53:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 5, 2007, 11:53pm UTC](https://www.ruby-forum.com/t/gsub-question-not-a-regex-question-including-part-of-the/113896/1 "2007-09-05T23:53:19Z")

</div>

Hi all,

I’ve got a simple bulletin board type of code in part of my  
application, and I want to implement phpbb-like tags for users to add  
to posts. I strip HTML out of the posts, of course. I can handle  
things like [b] =\> **with a simple gsub, but there’s one that is  
throwing me for a loop. I want users to be able to quote other users,  
and have the quoted text show up with an “originally posted by  
username” and some CSS code. I can gsub the [/QUOTE] into a   
but how do I change [QUOTE=username] into:**

_Originally posted by username_

Yes, I can do most of it with a gsub, but how do I pull “username” out  
of the original text and put it where it belongs in the substituted  
text?

Or is there a better way to do this all together?

Thanks!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 6, 2007, 6:21am UTC](https://www.ruby-forum.com/t/gsub-question-not-a-regex-question-including-part-of-the/113896/2 "2007-09-06T06:21:53Z")

</div>

removed\_email\_address@domain.invalid wrote:

> _Originally posted by username_
> 
> Yes, I can do most of it with a gsub, but how do I pull “username” out  
> of the original text and put it where it belongs in the substituted  
> text?
> 
> Shortened to not wrap:
> 
> “[q=u]”.gsub(/[q=(\w+)]/, ‘
> 
> blah \1
> ’)
> 
> =\> “\<div class=“q”\>blah u
> 
> ”
> 
> ( ) are used for grouping in regexes and \1, \2, etc. are used to  
> reference the groups in the replacement pattern.
> 
> So (\w+) creates a group of all the “word” characters after the q= and  
> before the ] and puts that into \1.
> 
> –  
> Michael W.
