From: Justin Collins Date: 2007-12-04T11:40:30+09:00 Subject: Re: Ruby regexp Match John Sheahan wrote: > I am trying to convert some old Perl scripts to Ruby and am stuck at a > spot where my Perl script is doing a regular expression match on some > text pulled from a webpage. > > In Perl, I was able to grab the text I wanted by putting () around the > regular expression which represented the data I wanted, then access it > with the x[0] method. > > Is there something like this in Ruby? I was looking through the String > class and don't really see a method listed to do this. > > I want to be able to do a kind of match like: > > m/^Title([a-zA-Z0-9])//g > > and access what's inside the () with x[0]. > > Thanks > > jackster > Yes. irb(main):001:0> result = "Title1".match(/^Title([a-zA-Z0-9])/) => # irb(main):002:0> result[1] => "1" irb(main):003:0> $1 => "1" irb(main):004:0> Regexp.last_match[1] => "1" irb(main):005:0> $~[1] => "1" http://ruby-doc.org/core/classes/MatchData.html -Justin