Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Regexp: rubular VS match. Why is the result different ?

Reply
Thread Tools

Regexp: rubular VS match. Why is the result different ?

 
 
Ale Ds
Guest
Posts: n/a
 
      10-28-2009
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 !)

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/.

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      10-28-2009
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/

 
Reply With Quote
 
 
 
 
Chris Shea
Guest
Posts: n/a
 
      10-28-2009
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>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 !)
>
> 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/.


Alessandro,

You'll want the String#scan method (http://www.ruby-doc.org/core/
classes/String.html#M000812).

015:0> regexp = /<([^<>]+)>/
=> /<([^<>]+)>/
016:0> str = 'anystring<hour>anystring<min>anystring<sec>anystr ing'
=> "anystring<hour>anystring<min>anystring<sec>anystr ing"
017:0> str.scan(regexp)
=> [["hour"], ["min"], ["sec"]]

HTH,
Chris
 
Reply With Quote
 
Ale Ds
Guest
Posts: n/a
 
      10-28-2009
> 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/.

 
Reply With Quote
 
Ale Ds
Guest
Posts: n/a
 
      10-28-2009
> You'll want the String#scan method (http://www.ruby-doc.org/core/
> 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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Rubular Rocks Intransition Ruby 7 11-12-2009 06:06 PM
regex that works on rubular.com but not in my program Andreas Hansen Ruby 7 06-26-2009 06:56 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
why get different result of a simple code on different compiler? Tao Wang C++ 4 11-09-2005 01:29 PM
1. Ruby result: 101 seconds , 2. Java result:9.8 seconds, 3. Perl result:62 seconds Michael Tan Ruby 32 07-21-2005 03:23 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57