2009/10/28 Ale Ds <>:
> I have to capture by means of regexp the content between '<' and '>'
>
> as instance:
>
> str = 'anystring<hour>anystring<min>anystring<sec>anystr ing'
> I need the array['hour','min,'sec']
>
> I have written the regexp: /(<([^<>]+)>)+/
> and I have tested it in rubular.com site (It work !)
The "+" at the end is superfluous because this would match multiple
concatenated sequences like <xx><yyy> which you want as separate
items.
> I have run it in irb:
>>> /(<([^<>]+)>)+/.match('anystring<hour>anystring<min>anystring<sec >anystring')
> => #<MatchData "<hour>" 1:"<hour>" 2:"hour">
>>>
> As you can see match method return just the first match in MatchData obj
>
> Do you know why ?
That's the difference between #match and #scan. You want scan in your code.
irb(main):001:0> str = 'anystring<hour>anystring<min>anystring<sec>anystr ing'
=> "anystring<hour>anystring<min>anystring<sec>anystr ing"
irb(main):002:0> str.scan /<([^>]+)>/
=> [["hour"], ["min"], ["sec"]]
irb(main):003:0> str.scan /<([^>]+)>/ do |m| p m end
["hour"]
["min"]
["sec"]
=> "anystring<hour>anystring<min>anystring<sec>anystr ing"
irb(main):004:0> str.scan /<([^>]+)>/ do |m,| p m end
"hour"
"min"
"sec"
=> "anystring<hour>anystring<min>anystring<sec>anystr ing"
Kind regards
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/