Ale_Ds
(Ale Ds)
28 October 2009 14:12
1
I have to capture by means of regexp the content between '<' and '>'
as instance:
str = 'anystring<hour>anystring<min>anystring<sec>anystring'
I need the array['hour','min,'sec']
I have written the regexp: /(<([^<>]+)>)+/
and I have tested it in rubular.com site (It work !)
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 ?
thank you,
Alessandro
···
--
Posted via http://www.ruby-forum.com/\ .
Robert_K1
(Robert K.)
28 October 2009 14:27
2
I have to capture by means of regexp the content between '<' and '>'
as instance:
str = 'anystring<hour>anystring<min>anystring<sec>anystring'
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>anystring'
=> "anystring<hour>anystring<min>anystring<sec>anystring"
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>anystring"
irb(main):004:0> str.scan /<([^>]+)>/ do |m,| p m end
"hour"
"min"
"sec"
=> "anystring<hour>anystring<min>anystring<sec>anystring"
Kind regards
robert
···
2009/10/28 Ale Ds <alexdesi@gmail.com>:
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
Alessandro,
You'll want the String#scan method (RDoc Documentation
classes/String.html#M000812).
015:0> regexp = /<([^<>]+)>/
=> /<([^<>]+)>/
016:0> str = 'anystring<hour>anystring<min>anystring<sec>anystring'
=> "anystring<hour>anystring<min>anystring<sec>anystring"
017:0> str.scan(regexp)
=> [["hour"], ["min"], ["sec"]]
HTH,
Chris
···
On Oct 28, 10:12 am, Ale Ds <alexd...@gmail.com> wrote:
I have to capture by means of regexp the content between '<' and '>'
as instance:
str = 'anystring<hour>anystring<min>anystring<sec>anystring'
I need the array['hour','min,'sec']
I have written the regexp: /(<([^<>]+)>)+/
and I have tested it in rubular.com site (It work !)
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 ?
thank you,
Alessandro
--
Posted viahttp://www.ruby-forum.com/.
Ale_Ds
(Ale Ds)
28 October 2009 14:46
4
The "+" at the end is superfluous because this would match multiple
concatenated sequences like <xx><yyy> which you want as separate
items.
...
I agree with you
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.
...
yes, scan works !
thanks a lot,
Alessandro
···
--
Posted via http://www.ruby-forum.com/\ .
Ale_Ds
(Ale Ds)
28 October 2009 15:01
5
You'll want the String#scan method (RDoc Documentation
classes/String.html#M000812).
Yes, at first time I didn't find scan method because I searched it in
Match obj (instead of String obj)
thank you
Alessandro
···
--
Posted via http://www.ruby-forum.com/\ .