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

**URL:** <https://www.ruby-forum.com/t/regexp-rubular-vs-match-why-is-the-result-different/178050>\
**Category:** Ruby\
**Created:** [October 28, 2009, 3:12pm UTC](https://www.ruby-forum.com/t/regexp-rubular-vs-match-why-is-the-result-different/178050 "2009-10-28T15:12:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![spitfire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a5b964/32.png) [@spitfire](https://www.ruby-forum.com/u/spitfire)\
**Post date:** [October 28, 2009, 3:12pm UTC](https://www.ruby-forum.com/t/regexp-rubular-vs-match-why-is-the-result-different/178050/1 "2009-10-28T15:12:22Z")

</div>

I have to capture by means of regexp the content between ‘\<’ and ‘\>’

as instance:

str = ‘anystringanystringanystringanystring’  
I need the array[‘hour’,'min,‘sec’]

I have written the regexp: /(\<([^\<\>]+)\>)+/  
and I have tested it in [rubular.com](http://rubular.com) site (It work !)

I have run it in irb:

> > /(\<([^\<\>]+)\>)+/.match(‘anystringanystringanystringanystring’)  
> > =\> #\<MatchData “” 1:“” 2:“hour”\>

As you can see match method return just the first match in MatchData obj

Do you know why ?

thank you,  
Alessandro

---

<div class="post-metadata">

**Author:** ![spitfire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a5b964/32.png) [@spitfire](https://www.ruby-forum.com/u/spitfire)\
**Post date:** [October 28, 2009, 3:28pm UTC](https://www.ruby-forum.com/t/regexp-rubular-vs-match-why-is-the-result-different/178050/2 "2009-10-28T15:28:23Z")

</div>

2009/10/28 Ale Ds [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I have to capture by means of regexp the content between ‘\<’ and ‘\>’
> 
> as instance:
> 
> str = ‘anystringanystringanystringanystring’  
> I need the array[‘hour’,'min,‘sec’]
> 
> I have written the regexp: /(\<([^\<\>]+)\>)+/  
> and I have tested it in [rubular.com](http://rubular.com) site (It work !)

The “+” at the end is superfluous because this would match multiple  
concatenated sequences like which you want as separate  
items.

> I have run it in irb:
> 
> > > /(\<([^\<\>]+)\>)+/.match(‘anystringanystringanystringanystring’)  
> > > =\> #\<MatchData “” 1:“” 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 =  
‘anystringanystringanystringanystring’  
=\> “anystringanystringanystringanystring”  
irb(main):002:0\> str.scan /\<([^\>]+)\>/  
=\> [[“hour”], [“min”], [“sec”]]  
irb(main):003:0\> str.scan /\<([^\>]+)\>/ do |m| p m end  
[“hour”]  
[“min”]  
[“sec”]  
=\> “anystringanystringanystringanystring”  
irb(main):004:0\> str.scan /\<([^\>]+)\>/ do |m,| p m end  
“hour”  
“min”  
“sec”  
=\> “anystringanystringanystringanystring”

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![spitfire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a5b964/32.png) [@spitfire](https://www.ruby-forum.com/u/spitfire)\
**Post date:** [October 28, 2009, 3:31pm UTC](https://www.ruby-forum.com/t/regexp-rubular-vs-match-why-is-the-result-different/178050/3 "2009-10-28T15:31:19Z")

</div>

On Oct 28, 10:12 am, Ale Ds [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have run it in irb:\>\> /(\<([^\<\>]+)\>)+/.match(‘anystringanystringanystringanystring’)  
> Posted viahttp://www.ruby-forum.com/.  
> Alessandro,

You’ll want the String#scan method ([RDoc Documentation](http://www.ruby-doc.org/core/)  
classes/String.html#M000812).

015:0\> regexp = /\<([^\<\>]+)\>/  
=\> /\<([^\<\>]+)\>/  
016:0\> str = ‘anystringanystringanystringanystring’  
=\> “anystringanystringanystringanystring”  
017:0\> str.scan(regexp)  
=\> [[“hour”], [“min”], [“sec”]]

HTH,  
Chris

---

<div class="post-metadata">

**Author:** ![spitfire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a5b964/32.png) [@spitfire](https://www.ruby-forum.com/u/spitfire)\
**Post date:** [October 28, 2009, 3:46pm UTC](https://www.ruby-forum.com/t/regexp-rubular-vs-match-why-is-the-result-different/178050/4 "2009-10-28T15:46:57Z")

</div>

> The “+” at the end is superfluous because this would match multiple  
> concatenated sequences like which you want as separate  
> items.  
> …  
> I agree with you

> > I have run it in irb:
> > 
> > > > /(\<([^\<\>]+)\>)+/.match(‘anystringanystringanystringanystring’)  
> > > > =\> #\<MatchData “” 1:"" 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

---

<div class="post-metadata">

**Author:** ![spitfire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a5b964/32.png) [@spitfire](https://www.ruby-forum.com/u/spitfire)\
**Post date:** [October 28, 2009, 4:01pm UTC](https://www.ruby-forum.com/t/regexp-rubular-vs-match-why-is-the-result-different/178050/5 "2009-10-28T16:01:42Z")

</div>

> You’ll want the String#scan method ([RDoc Documentation](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
