Forum: Ruby multiple matches

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
73700e119917433681f2e8f3e4369f74?d=identicon&s=25 Li CN (alex-osu3)
on 2009-05-28 15:34
Hi all,


How to find out pattern'cat'  in this string='ccccatgggctacatgggtcat'
? There are three 'cat' within the string and I want to  return  the
position of each match.

Although # match return the match , #pre_match and #post_match do the
tricks but they only work for the first match. How about the 2nd, third
match, and more?  I try #scan(pattern) but it doesn't return the
position for each match. I wonder how other handle this problem.

Thanks,

Li
Fbb4d027695dfdf76bf448b15d7e306a?d=identicon&s=25 matt neuburg (Guest)
on 2009-05-28 16:16
(Received via mailing list)
Li Chen <chen_li3@yahoo.com> wrote:

> I try #scan(pattern) but it doesn't return the
> position for each match. I wonder how other handle this problem.

It doesn't "return" the position but it does capture the position.

s = 'ccccatgggctacatgggtcat'
s.scan('cat') {puts Regexp.last_match.begin(0)}

The last regex match is always hanging around waiting for you to ask
about it.... Hope that helps - m.
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2009-05-28 16:52
(Received via mailing list)
On May 28, 2009, at 10:15 AM, matt neuburg wrote:

> The last regex match is always hanging around waiting for you to ask
> about it.... Hope that helps - m.
>
> --
> matt neuburg, phd = matt@tidbits.com, http://www.tidbits.com/matt/
> Leopard - http://www.takecontrolbooks.com/leopard-customizing.html
> AppleScript - http://www.amazon.com/gp/product/0596102119
> Read TidBITS! It's free and smart. http://www.tidbits.com

irb> s
=> "ccccatgggctacatgggtcat"
irb> pos = []
=> []
irb> while x = s.index('cat', (pos.last||-1)+1)
irb>   pos << x
irb>   end
=> nil
irb> pos
=> [3, 12, 19]

String#index is the way to go. The second arg is the position to start
looking for a match. [].last==nil so it is a small trick to make the
first loop test pass a 0 or 1 more than the previous match position.

-Rob


Rob Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
797ef431a5e1295b56c08e1db4c8d2df?d=identicon&s=25 botp (Guest)
on 2009-05-28 17:49
(Received via mailing list)
On Thu, May 28, 2009 at 9:34 PM, Li Chen <chen_li3@yahoo.com> wrote:
> How to find out pattern'cat'  in this string='ccccatgggctacatgggtcat'
> ? There are three 'cat' within the string and I want to  return  the position of each 
match.

there is String#index.
there is no indexes, but you can build one if you like

eg,

> s
=> "ccccatgggctacatgggtcat"
> class String
> def indexes str, n=0
>  n=index( str, n)
>   if n
>     [n] + indexes( str, n+=1)
>   else
>     []
>   end
> end
> end
=> nil
> s.indexes "cat"
=> [3, 12, 19]

the faster way is to loop, but i just want to test my recursion skill
;)

best regards
-botp
This topic is locked and can not be replied to.