Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Get Number of regex matches

Reply
Thread Tools

Get Number of regex matches

 
 
Ingo Weiss
Guest
Posts: n/a
 
      12-06-2006
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-forum.com/.

 
Reply With Quote
 
 
 
 
Max Muermann
Guest
Posts: n/a
 
      12-06-2006
On 12/7/06, Ingo Weiss <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Wilson Bilkovich
Guest
Posts: n/a
 
      12-06-2006
On 12/6/06, Ingo Weiss <(E-Mail Removed)> 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.

 
Reply With Quote
 
Martin DeMello
Guest
Posts: n/a
 
      12-06-2006
On 12/7/06, Max Muermann <(E-Mail Removed)> wrote:
> On 12/7/06, Ingo Weiss <(E-Mail Removed)> 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

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      12-07-2006
On 06.12.2006 22:44, Martin DeMello wrote:
> On 12/7/06, Max Muermann <(E-Mail Removed)> wrote:
>> On 12/7/06, Ingo Weiss <(E-Mail Removed)> 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
 
Reply With Quote
 
Ingo Weiss
Guest
Posts: n/a
 
      12-07-2006
Thank you very much for all your great suggestions!

Ingo

--
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
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
java.util.regex and multiple matches argabalala@yahoo.fr Java 4 09-28-2005 09:35 AM
regex, number of matches Dr.Ruud Perl Misc 31 09-27-2005 09:55 AM
regex: how to loop through individual matches darrel ASP .Net 1 01-03-2005 09:50 AM
Extracting matches from Regex.Split Stephan Bour ASP .Net 3 10-30-2003 04:59 PM



Advertisments