--0-468785399-12054140747735
Content-Type: text/plain; charset=us-ascii

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 Mckibbin <pmckibbin / gmail.com>
To: ruby-talk ML <ruby-talk / ruby-lang.org>
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
',endmark')
    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
',endmark')
    self.scan(Regexp.new(Regexp.escape(startmark)+'([^'+Regexp.escape(endmark)+']*)'+Regexp.escape(endmark))).flatten
  end
end

TESTxxxxa1byyyyya2bzzzzzzz'
extractedモヤョィァァャァァゥ
 ョ
モヤョィァァャァァゥ
 ョ
ヤナモヤイアイァ
モヤイョィァァャァァゥ
 ョ

「ア「ャ 「イ「ン
ロン
ロン
ュュ 
ミ  コッッョュョッョ





ュューュエカクキクオウケケュアイーオエアエーキエキキウオュュ