From: Naga Vijay Date: 2012-08-29T10:41:40+09:00 Subject: Regex Non Greedy Match Hello, I am trying to get a non-greedy match, but am unable to do so. What may I be doing wrong? Please help. ====== Code: ====== puts str1 = "Hi, @[Subject:13|My_Subject]" str2 = "Hi, @[Subject:13|My_Subject], and @[Subject:45|Your_Subject]" re = /@\[Subject:\d+\|(.*)\]/ # works fine, grabs the matching pattern p " ... this works fine ..." p str1.scan(re).flatten # why does this not grab the two patterns as an array? # how to avoid the greedy match? # that is, how to make it a non-greedy match? puts p " ... this does not work fine; how to avoid the greedy match? ..." p str2.scan(re).flatten puts ======== Output: ======== " ... this works fine ..." ["My_Subject"] " ... this does not work fine; how to avoid the greedy match? ..." ["My_Subject], and @[Subject:45|Your_Subject"] Thanks very much! Naga Vijay -- Posted via http://www.ruby-forum.com/.