[prev in list] [next in list] [prev in thread] [next in thread]
List: ruby-talk
Subject: Re: $-variables and regexp
From: Neil Stevens <neil () hakubi ! us>
Date: 2005-12-16 1:44:46
Message-ID: PMoof.3011$kd1.1079 () trnddc02
[Download RAW message or body]
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 Stevens - neil@hakubi.us
'A republic, if you can keep it.' -- Benjamin Franklin
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic