Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to find multiple matches in a string

Reply
Thread Tools

How to find multiple matches in a string

 
 
Alex DeCaria
Guest
Posts: n/a
 
      04-13-2010
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/.

 
Reply With Quote
 
 
 
 
Andrea Dallera
Guest
Posts: n/a
 
      04-13-2010
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



 
Reply With Quote
 
 
 
 
Intransition
Guest
Posts: n/a
 
      04-13-2010
On Apr 13, 12:51=A0pm, Alex DeCaria <(E-Mail Removed)>
wrote:
> I know how to use regular expressions to find the first match of a
> pattern in a string. =A0But, how do I most easily find multiple matches?
> For example, if the string is
>
> s =3D 'abcde_abcde_abcde'
>
> and I want to find the location of ALL the 'b' characters, how do I do
> it?


Try #scan

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      04-13-2010
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/
 
Reply With Quote
 
Alex DeCaria
Guest
Posts: n/a
 
      04-13-2010
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/.

 
Reply With Quote
 
Jesús Gabriel y Galán
Guest
Posts: n/a
 
      04-13-2010
On Tue, Apr 13, 2010 at 7:01 PM, Alex DeCaria
<(E-Mail Removed)> wrote:
> Andrea Dallera wrote:
>> Hei,
>>
>> =A0 s =3D 'abcde_abcde_abcde'
>> =A0 m =3D s.scan(/b/)
>> =A0 p m # ['b','b','b']
>>
>> HTH
>>
>> --
>> Andrea Dallera
>> http://github.com/bolthar/freightrain
>> http://usingimho.wordpress.com

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


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

http://blade.nagaokaut.ac.jp/cgi-bin...by-talk/337667

Jesus.

 
Reply With Quote
 
Andrea Dallera
Guest
Posts: n/a
 
      04-13-2010
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

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


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



 
Reply With Quote
 
Caleb Clausen
Guest
Posts: n/a
 
      04-13-2010
On 4/13/10, Alex DeCaria <(E-Mail Removed)> 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?


I think this works:

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

 
Reply With Quote
 
Andrea Dallera
Guest
Posts: n/a
 
      04-13-2010
Nope, tried it.

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


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



 
Reply With Quote
 
botp
Guest
Posts: n/a
 
      04-13-2010
On Wed, Apr 14, 2010 at 1:32 AM, Andrea Dallera
<(E-Mail Removed)> wrote:
> Nope, tried it.
>
> --
> Andrea Dallera
> http://github.com/bolthar/freightrain
> http://usingimho.wordpress.com
>
>
> On Wed, 2010-04-14 at 02:27 +0900, Caleb Clausen wrote:
>> s =3D 'abcde_abcde_abcde'
>> =A0 i=3D0
>> =A0 while i=3Ds.index('b',i)
>> =A0 =A0 =A0do_something_with i
>> =A0 end

>


what do you mean it does not work?

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

> s=3DStringScanner.new "this is a test string"

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

> p s.pos while s.scan_until /s/

4
7
13
16
=3D> nil


pls do not top post.

kind regards -botp

 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Check if String.matches() AND (if yes) extract number from String? Jochen Brenzlinger Java 5 11-21-2011 07:43 PM
regexp exclusion search - find matches NOT ending with a string? BrendanC Ruby 8 07-18-2009 07:34 PM
Checking a string against multiple matches Aaron Scott Python 7 12-02-2008 03:10 PM
best way to find matches (rookie alert) Jeff Kish Java 2 07-23-2004 02:07 AM



Advertisments