I'm looking for a method for the String class that returns an array of strings based on two input strings.
For example, if you had the string 'xxxxxa1byyyyya2bzzzzzzz' I'd like it to return the smaller strings between 'a' and b', but I don't want the smaller strings to include either 'a' or 'b'.
So, 'xxxxxa1byyyyya2bzzzzzzz'.method_x('a', 'b') would return ["1", "2"].
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).
I'm looking for a method for the String class that returns an array of strings based on two input strings.
For example, if you had the string 'xxxxxa1byyyyya2bzzzzzzz' I'd like it to return the smaller strings between 'a' and b', but I don't want the smaller strings to include either 'a' or 'b'.
So, 'xxxxxa1byyyyya2bzzzzzzz'.method_x('a', 'b') would return ["1", "2"].
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).
It may not be an exact fit for the calling syntax you want but you
could wrap it in something else.
David
--
Upcoming Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS, April 14-17 2008, New York City
CORE RAILS, June 24-27 2008, London (Skills Matter)
See http://www.rubypal.com for details. Berlin dates coming soon!
p method_x('xxxxxa1byyyyya2bzzzzzzz', 'a', 'b') #=> ["1", "2"].
# you could also extend the String class
class String
def method_x(first, last)
pattern = "#{first}(\\d)#{last}"
regexp = Regexp.new(pattern)
return self.scan(regexp).flatten
end
end
p 'xxxxxa1byyyyya2bzzzzzzz'.method_x('a', 'b')
···
On Thu, Mar 13, 2008 at 1:18 AM, Glenn <glenn_ritz@yahoo.com> wrote:
So, 'xxxxxa1byyyyya2bzzzzzzz'.method_x('a', 'b') would return ["1", "2"].
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