Use look-behind assertions in regular expressions with Ruby 1.8

Posted Almost 13 years ago. Visible to the public.

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.

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...
Henning Koch
Last edit
About 7 years ago
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2011-07-14 13:54)