# Regexp and using the matched value returned/found

**URL:** <https://www.ruby-forum.com/t/regexp-and-using-the-matched-value-returned-found/165363>\
**Category:** Ruby\
**Created:** [April 13, 2009, 5:56am UTC](https://www.ruby-forum.com/t/regexp-and-using-the-matched-value-returned-found/165363 "2009-04-13T05:56:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [April 13, 2009, 5:56am UTC](https://www.ruby-forum.com/t/regexp-and-using-the-matched-value-returned-found/165363/1 "2009-04-13T05:56:08Z")

</div>

Hi

I’m trying to do a regex search and using what I find a key to a Hash to  
dig up some formatting info.

But what’s happening is the literal \1 or \0 (or \1 \0) is not being  
resolved to what’s being found.

Any help would be awesome.

Ex: (Doesn’t work)

# regex looks like this: @swi\_start = /SWI((dm)|(ph)|(ty)|(st))st/

// executed in my code:  
line.gsub!( swi\_start, self.colorize\_token( ‘\1’ ) )

# also not working: line.gsub!( swi\_start, self.colorize\_token( ‘\1’

) )

def colorize\_token( event )  
puts “Event: " \<\< event  
return “\<font color=”#{@@color\_list[event]}”\>"  
end

Output:  
Event: \1

Ex: (This worked!!!)

line.gsub!( swi\_start, “#{@red\_start}\1#{@red\_end}”) then

Output:  
dm

Lastly please notes I’ve tried using " instead of ’ in my method call  
without any change.

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [April 13, 2009, 7:05am UTC](https://www.ruby-forum.com/t/regexp-and-using-the-matched-value-returned-found/165363/2 "2009-04-13T07:05:02Z")

</div>

Hi,

2009/4/13 Jean N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Ex: (Doesn’t work)  
> Â end
> 
> Lastly please notes I’ve tried using " instead of ’ in my method call  
> without any change.

You should use block format in that case  
Try  
line.gsub!(swi\_start){colorize\_token($1)}  
or  
line.gsub!(swi\_start){|x| colorize\_token(x)}

Regards,

Park H.

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [April 13, 2009, 4:49pm UTC](https://www.ruby-forum.com/t/regexp-and-using-the-matched-value-returned-found/165363/3 "2009-04-13T16:49:41Z")

</div>

Excellent all

Thank you for the info. Using the block based notation worked much  
better. (Also, Robert thank you for the additional info, very  
informative).

My next question is:

Can someone direct me to a link to reading materials (my Google skills  
seem to stink as I am not finding what I expect) on the pattern matching  
variables I can use like $1 mentioned above as I am now trying to match  
the WHOLE pattern and not just the “dm,ph,ty,st” portion.

So when I use:

line.gsub!(swi\_start){colorize\_token($1)}

The $1 only returns the aforementioned 2 char strings, I’d like to get  
the whole thing “SWIdmst” or “SWItynd” etc etc.

Thanks.

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [April 13, 2009, 5:22pm UTC](https://www.ruby-forum.com/t/regexp-and-using-the-matched-value-returned-found/165363/4 "2009-04-13T17:22:42Z")

</div>

On Mon, Apr 13, 2009 at 8:19 PM, Jean N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> […]  
> Can someone direct me to a link to reading materials (my Google skills  
> seem to stink as I am not finding what I expect) on the pattern matching  
> variables I can use like $1 mentioned above as I am now trying to match  
> the WHOLE pattern and not just the “dm,ph,ty,st” portion.

The keywords seem to be ‘ruby pre-defined variables’…  
First two hits:  
[http://is.gd/saBm](http://is.gd/saBm)  
[http://is.gd/saDy](http://is.gd/saDy)

> […]  
> The $1 only returns the aforementioned 2 char strings, I’d like to get  
> the whole thing “SWIdmst” or “SWItynd” etc etc.

$&

solidarity,  
lasitha

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [April 13, 2009, 1:45pm UTC](https://www.ruby-forum.com/t/regexp-and-using-the-matched-value-returned-found/165363/5 "2009-04-13T13:45:56Z")

</div>

On 13.04.2009 05:56, Jean N. wrote:

> I’m trying to do a regex search and using what I find a key to a Hash to  
> dig up some formatting info.
> 
> But what’s happening is the literal \1 or \0 (or \1 \0) is not being  
> resolved to what’s being found.

Heesob gave you the solution already but I’d like to add a bit of  
explanation why this is the solution.

> Any help would be awesome.
> 
> Ex: (Doesn’t work)
> 
> # regex looks like this: @swi\_start = /SWI((dm)|(ph)|(ty)|(st))st/

The capturing groups are not needed. This is sufficient:

/SWI(dm|ph|ty|st)st/

> // executed in my code:  
> line.gsub!( swi\_start, self.colorize\_token( ‘\1’ ) )

You need to be aware that all method arguments are evaluated _before_  
the method is actually invoked. Since you need an argument for  
#colorize which depends on the current match this can _never_ work  
because the #colorize is finished before #gsub! even starts matching.

> Ex: (This worked!!!)
> 
> line.gsub!( swi\_start, “#{@red\_start}\1#{@red\_end}”) then

No, this does not do the same as you attempted to do with the code above  
because in this case the replacement cannot depend on the match. In  
other words, you always insert the same color.

> Output:  
> dm
> 
> Lastly please notes I’ve tried using " instead of ’ in my method call  
> without any change.

This is irrelevant in this case as the quote type does not affect the  
time of evaluation (see above).

Kind regards

robert
