# $-variables and regexp

**URL:** <https://www.ruby-forum.com/t/variables-and-regexp/50417>\
**Category:** Ruby\
**Created:** [December 16, 2005, 2:59am UTC](https://www.ruby-forum.com/t/variables-and-regexp/50417 "2005-12-16T02:59:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![akoSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/47e85d/32.png) [@akoSS](https://www.ruby-forum.com/u/akoSS)\
**Post date:** [December 16, 2005, 2:59am UTC](https://www.ruby-forum.com/t/variables-and-regexp/50417/1 "2005-12-16T02:59:35Z")

</div>

hello,

if i indeed want to start using MatchData instead of the “warts” such  
as $-variables in regular expressions, how do i go about writing code  
blocks for String#sub et al if i want to get a match for a particular  
group?

example:

puts ‘a-b-c-’.gsub(/(.)-/) { $1 + ‘\_’ }

thanks  
konstantin

---

<div class="post-metadata">

**Author:** ![akoSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/47e85d/32.png) [@akoSS](https://www.ruby-forum.com/u/akoSS)\
**Post date:** [December 16, 2005, 3:47am UTC](https://www.ruby-forum.com/t/variables-and-regexp/50417/2 "2005-12-16T03:47:58Z")

</div>

ako… wrote:

> if i indeed want to start using MatchData instead of the “warts” such  
> as $-variables in regular expressions, how do i go about writing code  
> blocks for String#sub et al if i want to get a match for a particular  
> group?
> 
> example:
> 
> puts ‘a-b-c-’.gsub(/(.)-/) { $1 + ‘\_’ }

This is long, but will do:

puts ‘a-b-c-’.gsub(/(.)-/) { Regexp.last\_match[1] + ‘\_’ }

It would be very nice if .gsub started yielding the actual MatchData  
object in 2.0.

---

<div class="post-metadata">

**Author:** ![akoSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/47e85d/32.png) [@akoSS](https://www.ruby-forum.com/u/akoSS)\
**Post date:** [December 16, 2005, 4:18am UTC](https://www.ruby-forum.com/t/variables-and-regexp/50417/3 "2005-12-16T04:18:02Z")

</div>

On Dec 15, 2005, at 7:46 PM, ako… wrote:

> hello,
> 
> if i indeed want to start using MatchData instead of the “warts” such  
> as $-variables in regular expressions, how do i go about writing code  
> blocks for String#sub et al if i want to get a match for a particular  
> group?
> 
> example:
> 
> puts ‘a-b-c-’.gsub(/(.)-/) { $1 + ‘\_’ }

The MatchData object is also placed in a global variable, but I won’t  
show it here since it is a punctuation variable and you requested  
something different.

Not a direct answer, but you can just use a replacement string in  
this case:

‘a-b-c-’.gsub(/(.)-/, ‘\1\_’)

Or even no regular expression at all:

‘a-b-c-’.tr(’-’, ‘\_’)

Hope that helps.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![akoSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/47e85d/32.png) [@akoSS](https://www.ruby-forum.com/u/akoSS)\
**Post date:** [December 17, 2005, 1:42pm UTC](https://www.ruby-forum.com/t/variables-and-regexp/50417/4 "2005-12-17T13:42:08Z")

</div>

> puts ‘a-b-c-’.gsub(/(.)-/) { |str| str + ‘\_’ }

i think this is incorrect. id appends an underscore. i need to replace  
it.

---

<div class="post-metadata">

**Author:** ![akoSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/47e85d/32.png) [@akoSS](https://www.ruby-forum.com/u/akoSS)\
**Post date:** [December 17, 2005, 1:42pm UTC](https://www.ruby-forum.com/t/variables-and-regexp/50417/5 "2005-12-17T13:42:07Z")

</div>

ako… wrote:

> hello,
> 
> if i indeed want to start using MatchData instead of the “warts” such  
> as $-variables in regular expressions, how do i go about writing code  
> blocks for String#sub et al if i want to get a match for a particular  
> group?
> 
> example:
> 
> puts ‘a-b-c-’.gsub(/(.)-/) { $1 + ‘\_’ }

puts ‘a-b-c-’.gsub(/(.)-/) { |str| str + ‘\_’ }

Though gsub never does give you access to MatchData. MatchData is used  
when you do something like this:

matchData = /([[:alpha:]]+):([[:alpha:]]+)/.match(‘abc:xyz’)  
=\> MatchData  
matchData[1]  
=\> ‘abc’  
matchData[2]  
=\> ‘xyz’

–  
Neil S. - removed\_email\_address@domain.invalid

‘A republic, if you can keep it.’ – Benjamin Franklin

---

<div class="post-metadata">

**Author:** ![akoSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/47e85d/32.png) [@akoSS](https://www.ruby-forum.com/u/akoSS)\
**Post date:** [December 18, 2005, 11:20pm UTC](https://www.ruby-forum.com/t/variables-and-regexp/50417/6 "2005-12-18T23:20:26Z")

</div>

ako… wrote:

> > puts ‘a-b-c-’.gsub(/(.)-/) { |str| str + ‘\_’ }
> 
> i think this is incorrect. id appends an underscore. i need to replace  
> it.

Ah, sorry, mis-read what yours did.

It appears that gsub has been written to depend on the magic variables,  
because it gives you no other means of accessing the MatchData.

So, to use gsub like this and get the MatchData, your guess is as good  
as mine as to how to get to it. Just hold down shift and pound on the  
keyboard until you guess the right bit of magic punctuation.

Sorry I couldn’t help more,

–  
Neil S. - removed\_email\_address@domain.invalid

‘A republic, if you can keep it.’ – Benjamin Franklin
