James Gray
12/16/2005 2:09:00 AM
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 Gray II