[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Get Number of regex matches

Ingo Weiss

12/6/2006 8:17:00 PM

Hi,

how can I get the NUMBER of matches for a regular expression in a given
string?

For example: for string 'Banana' and regex /a/ I should get '3'
(matches)

Thanks!
Ingo

--
Posted via http://www.ruby-....

6 Answers

Max Muermann

12/6/2006 9:02:00 PM

0

On 12/7/06, Ingo Weiss <ingoweiss@gmail.com> wrote:
> Hi,
>
> how can I get the NUMBER of matches for a regular expression in a given
> string?
>
> For example: for string 'Banana' and regex /a/ I should get '3'
> (matches)
>

Here's one way (but there are probably better ones):

"Banana".scan(/a/).size
=> 3

max

Wilson Bilkovich

12/6/2006 9:03:00 PM

0

On 12/6/06, Ingo Weiss <ingoweiss@gmail.com> wrote:
> Hi,
>
> how can I get the NUMBER of matches for a regular expression in a given
> string?
>
> For example: for string 'Banana' and regex /a/ I should get '3'
> (matches)
>

One way is:
'Banana'.scan(/a/).size

I'm not sure how to get it out of the MatchData returned by 'match' or
=~, unfortunately.

Martin DeMello

12/6/2006 9:45:00 PM

0

On 12/7/06, Max Muermann <ruby@muermann.org> wrote:
> On 12/7/06, Ingo Weiss <ingoweiss@gmail.com> wrote:
> > Hi,
> >
> > how can I get the NUMBER of matches for a regular expression in a given
> > string?
> >
> > For example: for string 'Banana' and regex /a/ I should get '3'
> > (matches)
> >
>
> Here's one way (but there are probably better ones):
>
> "Banana".scan(/a/).size
> => 3

This one skips the intermediate array construction in return for some
clunkiness:

i = 0
banana.scan(/a/) { i += 1}
i

martin

Robert Klemme

12/7/2006 9:03:00 AM

0

On 06.12.2006 22:44, Martin DeMello wrote:
> On 12/7/06, Max Muermann <ruby@muermann.org> wrote:
>> On 12/7/06, Ingo Weiss <ingoweiss@gmail.com> wrote:
>> > Hi,
>> >
>> > how can I get the NUMBER of matches for a regular expression in a given
>> > string?
>> >
>> > For example: for string 'Banana' and regex /a/ I should get '3'
>> > (matches)
>> >
>>
>> Here's one way (but there are probably better ones):
>>
>> "Banana".scan(/a/).size
>> => 3
>
> This one skips the intermediate array construction in return for some
> clunkiness:
>
> i = 0
> banana.scan(/a/) { i += 1}
> i

>> require 'enumerator'
=> true
>> "banana".to_enum(:scan, /a/).inject(0) {|s,| s+1}
=> 3

:-)

robert

Ingo Weiss

12/7/2006 2:32:00 PM

0

Thank you very much for all your great suggestions!

Ingo

--
Posted via http://www.ruby-....

jameshcunningham@gmail.com

12/7/2006 7:07:00 PM

0

On 2006-12-07 04:02:55 -0500, Robert Klemme <shortcutter@googlemail.com> said:

> On 06.12.2006 22:44, Martin DeMello wrote:
>> On 12/7/06, Max Muermann <ruby@muermann.org> wrote:
>>> On 12/7/06, Ingo Weiss <ingoweiss@gmail.com> wrote:
>>> > Hi,
>>> >
>>> > how can I get the NUMBER of matches for a regular expression in a given
>>> > string?
>>> >
>>> > For example: for string 'Banana' and regex /a/ I should get '3'
>>> > (matches)
>>> >
>>>
>>> Here's one way (but there are probably better ones):
>>>
>>> "Banana".scan(/a/).size
>>> => 3
>>
>> This one skips the intermediate array construction in return for some
>> clunkiness:
>>
>> i = 0
>> banana.scan(/a/) { i += 1}
>> i
>
> >> require 'enumerator'
> => true
> >> "banana".to_enum(:scan, /a/).inject(0) {|s,| s+1}
> => 3
>
> :-)
>
> robert

banana.split('').reject { |i| i if i != 'a' }.length

I want extra points for using "banana.split".

Best,
James