From: Alex DeCaria on
I know how to use regular expressions to find the first match of a
pattern in a string. But, how do I most easily find multiple matches?
For example, if the string is

s = 'abcde_abcde_abcde'

and I want to find the location of ALL the 'b' characters, how do I do
it? If I use

m = s.match(/b/)

then m.offset(0) => [1,2]

but I don't have any information about the second or third occurences of
'b'.

--Alex
--
Posted via http://www.ruby-forum.com/.

From: Andrea Dallera on
Hei,

s = 'abcde_abcde_abcde'
m = s.scan(/b/)
p m # ['b','b','b']

HTH

--
Andrea Dallera
http://github.com/bolthar/freightrain
http://usingimho.wordpress.com


On Wed, 2010-04-14 at 01:51 +0900, Alex DeCaria wrote:
> I know how to use regular expressions to find the first match of a
> pattern in a string. But, how do I most easily find multiple matches?
> For example, if the string is
>
> s = 'abcde_abcde_abcde'
>
> and I want to find the location of ALL the 'b' characters, how do I do
> it? If I use
>
> m = s.match(/b/)
>
> then m.offset(0) => [1,2]
>
> but I don't have any information about the second or third occurences of
> 'b'.
>
> --Alex


From: Intransition on
On Apr 13, 12:51 pm, Alex DeCaria <alex.deca...(a)millersville.edu>
wrote:
> I know how to use regular expressions to find the first match of a
> pattern in a string.  But, how do I most easily find multiple matches?
> For example, if the string is
>
> s = 'abcde_abcde_abcde'
>
> and I want to find the location of ALL the 'b' characters, how do I do
> it?

Try #scan

From: Robert Klemme on
On 04/13/2010 06:55 PM, Andrea Dallera wrote:
> Hei,
>
> s = 'abcde_abcde_abcde'
> m = s.scan(/b/)
> p m # ['b','b','b']

Or even

s.scan(/b) {|match| p match}

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
From: Alex DeCaria on
Andrea Dallera wrote:
> Hei,
>
> s = 'abcde_abcde_abcde'
> m = s.scan(/b/)
> p m # ['b','b','b']
>
> HTH
>
> --
> Andrea Dallera
> http://github.com/bolthar/freightrain
> http://usingimho.wordpress.com

Thanks Andrea. But is there a way to also find the locations (indexes)
of the 'b' characters?

--Alex
--
Posted via http://www.ruby-forum.com/.