How to find multiple matches in a string

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/.

Hei,

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

HTH

···

--
Andrea Dallera

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

Try #scan

···

On Apr 13, 12:51 pm, Alex DeCaria <alex.deca...@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?

Andrea Dallera wrote:

Hei,

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

HTH

--
Andrea Dallera
GitHub - bolthar/freightrain: Ruby desktop development made easy
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/\.

Or even

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

Kind regards

  robert

···

On 04/13/2010 06:55 PM, Andrea Dallera wrote:

Hei,

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

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Take a look at this thread, maybe you can get some ideas:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/337667

Jesus.

···

On Tue, Apr 13, 2010 at 7:01 PM, Alex DeCaria <alex.decaria@millersville.edu> wrote:

Andrea Dallera wrote:

Hei,

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

HTH

--
Andrea Dallera
GitHub - bolthar/freightrain: Ruby desktop development made easy
http://usingimho.wordpress.com

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

string = "abcde abcde abcde"
sum = 0
result =
blocks = string.split(/b/)
blocks.pop
blocks.each do |block|
  sum += block.length
  result << sum
  sum += 1
end
p result # [1,7,13]

Don't tell anyone I've written this crap :slight_smile:

···

--
Andrea Dallera

On Wed, 2010-04-14 at 02:01 +0900, Alex DeCaria wrote:

Andrea Dallera wrote:
> Hei,
>
> s = 'abcde_abcde_abcde'
> m = s.scan(/b/)
> p m # ['b','b','b']
>
> HTH
>
> --
> Andrea Dallera
> GitHub - bolthar/freightrain: Ruby desktop development made easy
> http://usingimho.wordpress.com

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

--Alex

I think this works:

  s = 'abcde_abcde_abcde'
  i=0
  while i=s.index('b',i)
     do_something_with i
  end

···

On 4/13/10, Alex DeCaria <alex.decaria@millersville.edu> wrote:

Andrea Dallera wrote:

Hei,

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

HTH

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

irb(main):012:0> s="a "*5
=> "a a a a a "
irb(main):013:0> s.scan(/a+/) { p $~, $~.offset(0) }
#<MatchData "a">
[0, 1]
#<MatchData "a">
[2, 3]
#<MatchData "a">
[4, 5]
#<MatchData "a">
[6, 7]
#<MatchData "a">
[8, 9]
=> "a a a a a "
irb(main):014:0>

Kind regards

  robert

···

On 13.04.2010 19:01, Alex DeCaria wrote:

Andrea Dallera wrote:

Hei,

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

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

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Nope, tried it.

···

--
Andrea Dallera

On Wed, 2010-04-14 at 02:27 +0900, Caleb Clausen wrote:

s = 'abcde_abcde_abcde'
  i=0
  while i=s.index('b',i)
     do_something_with i
  end

Nope, tried it.

--
Andrea Dallera
GitHub - bolthar/freightrain: Ruby desktop development made easy
http://usingimho.wordpress.com

s = 'abcde_abcde_abcde'
i=0
while i=s.index('b',i)
do_something_with i
end

what do you mean it does not work?

btw, you can also use stringscan. it's fast.

s=StringScanner.new "this is a test string"

=> #<StringScanner 0/21 @ "this ...">

p s.pos while s.scan_until /s/

4
7
13
16
=> nil

pls do not top post.

kind regards -botp

···

On Wed, Apr 14, 2010 at 1:32 AM, Andrea Dallera <andrea@andreadallera.com> wrote:

On Wed, 2010-04-14 at 02:27 +0900, Caleb Clausen wrote: