Forum: Ruby how to find the position of each match within a string

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-01-24 20:46
Hi all,

I have  a small script to find a match. If a match is found I need to
print its position within the string. I try the String#index but it only
returns the first match. Any idea?

Thanks,

Li

C:\Documents and Settings\chen73\My Documents\Ruby\bin>irb
irb(main):001:0> "ello-hello".scan('e') do |match|
irb(main):002:1*     p "ello-hello".index(match)
irb(main):003:1> end
0
0
=> "ello-hello"
irb(main):004:0>
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2009-01-24 21:01
(Received via mailing list)
Hi --

On Sun, 25 Jan 2009, Li Chen wrote:

>
> C:\Documents and Settings\chen73\My Documents\Ruby\bin>irb
> irb(main):001:0> "ello-hello".scan('e') do |match|
> irb(main):002:1*     p "ello-hello".index(match)
> irb(main):003:1> end
> 0
> 0
> => "ello-hello"

You can examine the $~ global MatchData object, in a couple of ways:

irb(main):003:0> "ello-hello".scan('e') do |match|
irb(main):004:1*   p $~.pre_match.size
irb(main):005:1> end
0
6
=> "ello-hello"
irb(main):006:0> "ello-hello".scan('e') do |match|
irb(main):007:1*   p $~.offset(0)[0]
irb(main):008:1> end
0
6
=> "ello-hello

(And there may be more I'm not thinking of.)


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

http://www.wishsight.com => Independent, social wishlist management!
699c00ad35f2755810b4aa5f423d73e2?d=identicon&s=25 Albert Schlef (alby)
on 2009-01-25 00:33
> You can examine the $~ global MatchData object, in a couple of ways:
>
> irb(main):003:0> "ello-hello".scan('e') do |match|
> irb(main):004:1*   p $~.pre_match.size
> irb(main):005:1> end
> 0
> 6

Interesting. Does that mean that String#scan() converts a String
argument to RegExp?
Aaca034456897ccbc8bb14953c4a41c1?d=identicon&s=25 Radosław Bułat (radarek)
on 2009-01-25 00:44
(Received via mailing list)
On Sun, Jan 25, 2009 at 12:31 AM, Albert Schlef <albertschlef@gmail.com>
wrote:
>> You can examine the $~ global MatchData object, in a couple of ways:
>>
>> irb(main):003:0> "ello-hello".scan('e') do |match|
>> irb(main):004:1*   p $~.pre_match.size
>> irb(main):005:1> end
>> 0
>> 6
>
> Interesting. Does that mean that String#scan() converts a String
> argument to RegExp?

Yes it does, even if it search fixed-size string.


--
Pozdrawiam

Rados³aw Bu³at
http://radarek.jogger.pl - mój blog
699c00ad35f2755810b4aa5f423d73e2?d=identicon&s=25 Albert Schlef (alby)
on 2009-01-25 01:42
Radosław Bułat wrote:
>>> You can examine the $~ global MatchData object, in a couple of ways:
>>>
>>> irb(main):003:0> "ello-hello".scan('e') do |match|
>>> irb(main):004:1*   p $~.pre_match.size
>>> irb(main):005:1> end
>>> 0
>>> 6
>>
>> Interesting. Does that mean that String#scan() converts a String
>> argument to RegExp?
>
> Yes it does, even if it search fixed-size string.

I'm shocked. Why does it do this? (this conversion)

And doesn't this mean that #scan is less efficient than it could have
been? Isn't matching regexps slower than matching plain strings?
73700e119917433681f2e8f3e4369f74?d=identicon&s=25 Li CN (alex-osu3)
on 2009-01-25 15:35
David A. Black wrote:
> You can examine the $~ global MatchData object, in a couple of ways:
>
> irb(main):003:0> "ello-hello".scan('e') do |match|
> irb(main):004:1*   p $~.pre_match.size
> irb(main):005:1> end
> 0
> 6
> => "ello-hello"
> irb(main):006:0> "ello-hello".scan('e') do |match|
> irb(main):007:1*   p $~.offset(0)[0]
> irb(main):008:1> end
> 0
> 6
> => "ello-hello

Hi David,

Thanks.

I go back and read the chapter about MatchData. And it helps me a lot.

Li
This topic is locked and can not be replied to.