Thursday, April 12, 2007

A nifty Ruby regexp trick

Just hit upon this while working on a rails project. Thought it might be worth sharing with others in case someone is interested.
The idea is as follows ->
suppose one defines a simple regular expression as follows

r = '(a)|(b)|(c)'

re = Regexp.new(r)


This re can be used with either String.match. The String.match returns a MatchData object. Often one is interested in an array. which can be obtained by String.scan instead of String.match. However this array is actually an array of Arrays, with "nil" at places where the string doesn't match..
eg.

"abhijit".scan(re)

will return

[["a", nil, nil], [nil, "b", nil], [nil, nil, "h"]]

If one just wants the array as ["a", "b", "c"], one could use following trick.

"abhijit".scan(re).flatten.compact

The flatten flattens out the array and compact gets rid of all nil
pretty useful .. One can then iterate over this array. Somehow this convenience is not offered by MatchData or atleast I don't know of it.

No comments: