From: 7stud -- Date: 2012-12-21T04:55:46+09:00 Subject: Re: Perl to Ruby: regex captures to assignment. Derrick B. wrote in post #1089748: > > I am prompting for input, so I would be parsing individual strings and > not a list of them. Though I will remember that for future static > testing. Also, I and using the "\S+" to handle negative rational > numbers: "-1 3/4". > test_data = [ "3/4", "1 3/4", "-1 3/4", 'hello' ] test_data.each do |str| match_data = str.match( %r{ ( [-]? \d+ )? \s* (\d+) / (\d+) }xms ) w, n, d = match_data ? match_data.captures: [str, 0, 1] puts "%s %s %s" % [w, n, d] end --output:-- 3 4 1 3 4 -1 3 4 hello 0 1 -- Posted via http://www.ruby-forum.com/.