Playing with Oniguruma
I’ve been busy reading Jeffrey Friedl’s Mastering Regular Expressions and getting a little sad that some of the coolest tricks available are not (yet) available in Ruby. Oniguruma, the regular expression engine coming in Ruby 2.0, is more feature-full and faster than what we have now, and makes whole swaths of Mastering Regular Expressions suddenly relevant. I hear it’s possible to recompile 1.8 to use Oniguruma instead, but I’m not quite ready for that.
I am ready for lookbehind and named captures, though. Thankfully, the Oniguruma gem is available to save me from trying to mess up my Ruby install.
The one unsettling thing about using the Oniguruma gem, though, is how they left String’s match method alone. It’s regexp.match(string) only for these things. Thankfully, that’s easily fixed:
class String def o_match(regexp) case regexp when Oniguruma::ORegexp regexp.match(self) else old_match(regexp) end end alias_method :old_match, :match alias_method :match, :o_match end
andy eggers said,
August 2, 2007 @ 11:34 am
Thanks for this post. I wanted to use lookbehinds too and was surprised to see Ruby’s built-in regex doesn’t support it. Your post helped me quickly get going with the oniguruma gem.
Another interesting stumbling block was that the “synopsis” section of the README (http://oniguruma.rubyforge.org/oniguruma/) mistakenly calls the class “ORegex” instead of “ORegexp.”
Dizan Vasquez said,
September 19, 2007 @ 7:52 am
Hi guys, just wanted to say that both issuers have been fixed already in the current version of Oniguruma