# How to find the position of each match within a string

**URL:** <https://www.ruby-forum.com/t/how-to-find-the-position-of-each-match-within-a-string/158558>\
**Category:** Ruby\
**Created:** [January 24, 2009, 8:46pm UTC](https://www.ruby-forum.com/t/how-to-find-the-position-of-each-match-within-a-string/158558 "2009-01-24T20:46:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [January 24, 2009, 8:46pm UTC](https://www.ruby-forum.com/t/how-to-find-the-position-of-each-match-within-a-string/158558/1 "2009-01-24T20:46:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [January 24, 2009, 9:01pm UTC](https://www.ruby-forum.com/t/how-to-find-the-position-of-each-match-within-a-string/158558/2 "2009-01-24T21:01:33Z")

</div>

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](http://www.rubypal.com)  
Coming in 2009: The Well-Grounded Rubyist ([The Well-Grounded Rubyist](http://manning.com/black2))

[http://www.wishsight.com](http://www.wishsight.com) =\> Independent, social wishlist management!

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [January 25, 2009, 12:33am UTC](https://www.ruby-forum.com/t/how-to-find-the-position-of-each-match-within-a-string/158558/3 "2009-01-25T00:33:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [January 25, 2009, 1:42am UTC](https://www.ruby-forum.com/t/how-to-find-the-position-of-each-match-within-a-string/158558/4 "2009-01-25T01:42:20Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [January 25, 2009, 12:44am UTC](https://www.ruby-forum.com/t/how-to-find-the-position-of-each-match-within-a-string/158558/5 "2009-01-25T00:44:02Z")

</div>

On Sun, Jan 25, 2009 at 12:31 AM, Albert S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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](http://radarek.jogger.pl) - mój blog

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [January 25, 2009, 3:35pm UTC](https://www.ruby-forum.com/t/how-to-find-the-position-of-each-match-within-a-string/158558/6 "2009-01-25T15:35:09Z")

</div>

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
