From: "Nik Z." Date: 2011-08-25T09:19:59+09:00 Subject: Re: how to find all matches using ruby regex? You never set the 2nd set of parens (...) in the original regex: (data-\d+\.\d+\.\d+\.tar\.gz)/ therefore, no $2 for you... change to regex to /(data\S+) and (data\S+)/, and you get $1 and $2... michael xu wrote in post #1018329: > I have the following ruby code and I want to find all matched > "data-1.2.5.tar.gz" and "data-1.2.7.tar.gz", then stored in $1 and $2. > When I executed it and I found $1 is "data-1.2.5.tar.gz", but $2 is nil. > How can I get $2 is "data-1.2.7.tar.gz"? > > ============================================================= > string1 = "data-1.2.5.tar.gz and data-1.2.7.tar.gz and readme." > > if string1 =~ /(data-\d+\.\d+\.\d+\.tar\.gz)/ > print "Matched on ", $1, "\n" > print "Matched on ", $2, "\n" > > else > puts "NO MATCH" > end -- Posted via http://www.ruby-forum.com/.