Archive for April, 2008

h1

Match and capture multiple occurrences of strings in Ruby

April 29, 2008

The real reason I started this blog – to publish little snippets of code! I owe my sensei for help with this one.

class Regexp

    # Matches ALL occurrences of a regular expression in a string.
    # Returns an array of the matches as strings.
    # The regular expression must explicitly use groupings [ like (capturethis) ]
    # to capture matching strings.
    def matchAll( string )
      matchArray = [];
      work_str = string;

      while (m = self.match( work_str ) )
        matchArray << m.captures[0];
        work_str = m.post_match;
      end
      return matchArray;
    end
  end

Advertisements
h1

About the name

April 29, 2008

http://en.wikipedia.org/wiki/Fungibility#Fungibility_in_typography

Fleeting and capable of mutual substitution. Yep.