# Re: String method to take subsections

**URL:** <https://www.ruby-forum.com/t/re-string-method-to-take-subsections/131810>\
**Category:** Ruby\
**Created:** [March 13, 2008, 2:15pm UTC](https://www.ruby-forum.com/t/re-string-method-to-take-subsections/131810 "2008-03-13T14:15:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [March 13, 2008, 2:15pm UTC](https://www.ruby-forum.com/t/re-string-method-to-take-subsections/131810/1 "2008-03-13T14:15:43Z")

</div>

Thanks for your help, guys. All three suggestions are great.

It seems to me that returning an empty array instead of nil is actually  
better.

Glenn

----- Original Message ----  
From: Paul M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: ruby-talk ML [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Wednesday, March 12, 2008 9:41:26 PM  
Subject: Re: String method to take subsections

Glenn wrote:

> Hi,
> 
> I’m looking for a method for the String class that returns an array of  
> strings based on two input strings.

Regular expressions are your friend here:

class String  
def containedby(startmark=‘a’,endmark=‘b’)  
self.scan(Regexp.new(startmark+‘([^’+endmark+‘]\*)’+endmark)).flatten  
end  
end

should do what you want. If you want to restrict it to just one  
character remove the \* in ‘]\*)’.

> If you had the string ‘a1b2’ and tried ‘a1b2’.method\_x(‘a’, ‘c’) you’d  
> get nil because ‘c’ is obviously not in the String that’s calling the  
> method.
> 
> Likewise, if you tried ‘a1b2’.method\_x(‘b’, ‘a’) it would also return  
> nil since there’s no string between ‘b’ and ‘a’ (so the order matters).

Actually returns [] in both these cases, but I’m sure you can deal with  
those.

# Test Listing and output below:

class String  
def containedby(startmark=‘a’,endmark=‘b’)  
self.scan(Regexp.new(Regexp.escape(startmark)+‘([^’+Regexp.escape(endmark)+‘]\*)’+Regexp.escape(endmark))).flatten  
end  
end

TEST=‘xxxxxa1byyyyya2bzzzzzzz’  
extracted=TEST.containedby(‘a’,‘b’)  
puts extracted.inspect  
extracted=TEST.containedby(‘a’,‘c’)  
puts extracted.inspect  
TEST2=‘b1a2’  
extracted=TEST2.containedby(‘a’,‘b’)  
puts extracted.inspect

=====================================

[“1”, “2”]  
[]  
[]

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [March 13, 2008, 9:51pm UTC](https://www.ruby-forum.com/t/re-string-method-to-take-subsections/131810/2 "2008-03-13T21:51:06Z")

</div>

Glenn wrote:

> Thanks for your help, guys. All three suggestions are great.
> 
> It seems to me that returning an empty array instead of nil is actually  
> better.

I’d be inclined to agree, although some might argue that a test for nil  
is easier.

class String  
def containedby(startmark=‘a’,endmark=‘b’)  
x=self.scan(Regexp.new(Regexp.escape(startmark)+’([^’+Regexp.escape(endmark)+’]\*)’+Regexp.escape(endmark))).flatten  
x.size \> 0 ? x : nil  
end  
end

would return the error conditions as nil.

Mac
