Read more

Use look-behind assertions in regular expressions with Ruby 1.8

Henning Koch
July 14, 2011Software engineer at makandra GmbH

Regular expressions can have something called "zero-width look-behind assertions". This means that you want a pattern to be preceded by another pattern, but not include the preceding pattern in your match or search cursor. E.g. (?<=x)y matches y in xyz but not in syz. There are also negative look-behind assertions, e.g. (?<!x)y matches y in syz but not in xyz.

Illustration online protection

Rails professionals since 2007

Our laser focus on a single technology has made us a leader in this space. Need help?

  • We build a solid first version of your product
  • We train your development team
  • We rescue your project in trouble
Read more Show archive.org snapshot

Unfortunately look-behind assertions are only available in Ruby 1.9. With Ruby 1.8 you need to use an alternative regular expression library called Oniguruma Show archive.org snapshot . This is the library used by Ruby 1.9, packaged into a gem. We have a note on how to install Oniguruma.

You can now instantiate an Oniguruma pattern like this:

pattern = Oniguruma::ORegexp.new('(?<=x)y')

You cannot feed use this pattern into methods that expect a vanilla Ruby Regexp. Instead an Oniguruma pattern comes with all the methods you need:

pattern.match('string')
pattern.gsub('string', 'replacement')
# etc...
Posted by Henning Koch to makandra dev (2011-07-14 15:54)