# Substring

**URL:** <https://rubytalk.org/t/substring/74095>\
**Category:** ruby-talk\
**Created:** [12 March 2017 23:04 UTC](https://rubytalk.org/t/substring/74095 "2017-03-12T23:04:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Die\_Optimisten](https://avatars.discourse-cdn.com/v4/letter/d/f04885/32.png) [@Die\_Optimisten](https://rubytalk.org/u/Die_Optimisten)\
**Post date:** [12 March 2017 23:04 UTC](https://rubytalk.org/t/substring/74095/1 "2017-03-12T23:04:22Z")

</div>

Hi

How can I get begin- and END-positions (or length) of all matches? (for different substrings)  
(is there a trick as with case?)  
pos=0; string.each {... } # comparing manually char by char is not nice

thank you  
Opti

---

<div class="post-metadata">

**Author:** ![stomar](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@stomar](https://rubytalk.org/u/stomar)\
**Post date:** [13 March 2017 05:32 UTC](https://rubytalk.org/t/substring/74095/2 "2017-03-13T05:32:01Z")

</div>

MatchData, which is returned from Regexp#match,  
has all the information.

Regards,  
Marcus

> **···**
>
> Am 13.03.2017 um 00:04 schrieb Die Optimisten:
> 
> > How can I get begin- and END-positions (or length) of all matches? (for  
> > different substrings)  
> > (is there a trick as with case?)
> 
> --  
> GitHub: [stomar (Marcus Stollsteimer) · GitHub](https://github.com/stomar/)  
> PGP: 0x6B3A101A

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [13 March 2017 11:53 UTC](https://rubytalk.org/t/substring/74095/3 "2017-03-13T11:53:53Z")

</div>

You can also use String#scan and access global variables.

irb(main):014:0\> "foobar".scan(/./) { p [$`, $&, $'] }  
["", "f", "oobar"]  
["f", "o", "obar"]  
["fo", "o", "bar"]  
["foo", "b", "ar"]  
["foob", "a", "r"]  
["fooba", "r", ""]  
=\> "foobar"

String lengths can be used for numeric positions.

Cheers

robert

> **···**
>
> On Mon, Mar 13, 2017 at 6:32 AM, \<sto.mar@web.de\> wrote:
> 
> > Am 13.03.2017 um 00:04 schrieb Die Optimisten:
> > 
> > > How can I get begin- and END-positions (or length) of all matches? (for  
> > > different substrings)  
> > > (is there a trick as with case?)
> > 
> > MatchData, which is returned from Regexp#match,  
> > has all the information.
> 
> --  
> [guy, jim, charlie].each {|him| remember.him do |as, often| as.you\_can  
> - without end}  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [13 March 2017 11:57 UTC](https://rubytalk.org/t/substring/74095/4 "2017-03-13T11:57:07Z")

</div>

Forgot one

irb(main):026:0\> "foobar".scan(/./) { p [$`, $&, $', $~] }  
["", "f", "oobar", #\<MatchData "f"\>]  
["f", "o", "obar", #\<MatchData "o"\>]  
["fo", "o", "bar", #\<MatchData "o"\>]  
["foo", "b", "ar", #\<MatchData "b"\>]  
["foob", "a", "r", #\<MatchData "a"\>]  
["fooba", "r", "", #\<MatchData "r"\>]  
=\> "foobar"

> **···**
>
> On Mon, Mar 13, 2017 at 12:53 PM, Robert Klemme \<shortcutter@googlemail.com\> wrote:
> 
> > On Mon, Mar 13, 2017 at 6:32 AM, \<sto.mar@web.de\> wrote:
> > 
> > > Am 13.03.2017 um 00:04 schrieb Die Optimisten:
> > > 
> > > > How can I get begin- and END-positions (or length) of all matches? (for  
> > > > different substrings)  
> > > > (is there a trick as with case?)
> > > 
> > > MatchData, which is returned from Regexp#match,  
> > > has all the information.
> > 
> > You can also use String#scan and access global variables.
> > 
> > irb(main):014:0\> "foobar".scan(/./) { p [$`, $&, $'] }  
> > ["", "f", "oobar"]  
> > ["f", "o", "obar"]  
> > ["fo", "o", "bar"]  
> > ["foo", "b", "ar"]  
> > ["foob", "a", "r"]  
> > ["fooba", "r", ""]  
> > =\> "foobar"
> > 
> > String lengths can be used for numeric positions.
> 
> --  
> [guy, jim, charlie].each {|him| remember.him do |as, often| as.you\_can  
> - without end}  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)
