From: Brian Candler Date: 2012-04-10T04:31:27+09:00 Subject: Re: How to pull out specific part of a variable and set to another variable? "Iachetti Federico Martín" If there could be more than one match: > > pry(main)> new_var2 = *var.match(/({.*})/).captures* > => ["{inside these brackets}"] > pry(main)> This is wrong in several important ways: >> var = "abc {foo} def {bar}" => "abc {foo} def {bar}" >> var.match(/({.*})/).captures SyntaxError: compile error (irb):2: invalid regular expression; there's no previous pattern, to which '{' would define cardinality at 2: /({.*})/ from (irb):2 >> >> var.match(/(\{.*\})/).captures => ["{foo} def {bar}"] >> var.match(/(\{.*?\})/).captures => ["{foo}"] What I think you're looking for is: >> var.scan(/\{.*?\}/) => ["{foo}", "{bar}"] -- Posted via http://www.ruby-forum.com/.