From: Robert Klemme Date: 2008-04-07T17:06:41+09:00 Subject: Re: Regex and non-greedy matching? 2008/4/7, Marc Heiler : > I have a slight problem. I have strings with some tags such as > > 'name:' > > I need to match "name:" and "lightblue" > In other words: > - What is between <> > and > - What is inside the first <> right next to "name:" > > The following regex does not work: > > 'name:' =~ /<([a-zA-Z]+)>(.+?)<\/>/ > > $1 # => "b" > $2 # => "name: > > $2 should only be name: > and $1 should only be lightblue Constructing a regexp to match more specific often helps: irb(main):001:0> s='name:' => "name:" irb(main):002:0> md = %r{\s*<([^>]*)>([^<]*)}.match s => # irb(main):003:0> md.to_a => ["name:", "lightblue", "name:"] irb(main):004:0> md = %r{\s*<([^>]*)>\s*([^<]*)}.match s => # irb(main):005:0> md.to_a => ["name:", "lightblue", "name:"] irb(main):006:0> See how this works without reluctant quantifier? Cheers robert -- use.inject do |as, often| as.you_can - without end