Remember how, in Ruby, the scan method doesn’t return the full match, but only the captured groups? Thanks to some inspiration by Joshua, I’ve written a small method doing precisely this.
class String
def pscan(p)
r = []
a = self.scan(p) {|m| r.push(m.unshift("#{$&}")) }
if block_given?
yield(r)
else
r
end
end
end
This exposes a method, pscan which hands out arrays of matches that contain the full match as its first element, and the rest of the captured groups in sequence, like scan does. The method should carry the exact same interface as scan, including returning the same result when nothing matches, and follow every other semantic except for the inclusion of the full match as position 0.
Include as you will. I release this code into the public domain (why not?) but attribution (by linking to this post) is appreciated.
Update 2006-06-23: This method was originally called pcrescan and merely aliased as pscan. After realizing that the name could be confusion with respect to the PCRE regex library – which Ruby doesn’t use, it uses Oniguruma – and that pscan is briefer and a better name, I omitted the name pcrescan from the code entirely. My original intention was to show that the items returned in the array are returned as they are from similar methods/functions in other Perl-compatible regex libraries for other languages.
No comments yet.
Sorry, the comment form is closed at this time.