From: Robert Klemme Date: 2009-03-10T18:25:02+09:00 Subject: Re: Regexp problem 2009/3/10 The Higgs bozo : > Albert Schlef wrote: >> I want to extract an "id" parameter from the URL. I use the following >> code: >> >>   url = "http://example.com/id=12345" >>   id = url[ /(?<=id=)(\d+)/ ] >>   puts id >> >> But I get a SyntaxError telling me "undefined (?...) sequence". >> >> Why? > > Because ruby 1.8 does not have look-behind.  1.9 has it, and your code > runs on 1.9. > > If you need 1.8 with look-behind, I guess you can compile 1.8 with the > Oniguruma regexp engine (which is used in 1.9). Lookbehind is not needed: 09:35:18 ~$ irb Ruby version 1.8.7 irb(main):001:0> url = "http://example.com/id=12345" => "http://example.com/id=12345" irb(main):002:0> id = url[/id=(\d+)/, 1] => "12345" Depending on what else should be done with the url a more appropriate tool might be class URI irb(main):006:0> require 'uri' => true irb(main):007:0> u2 = URI.parse url => # irb(main):008:0> u2.path => "/id=12345" irb(main):009:0> u2.path[/id=(\d+)/, 1] => "12345" Kind regards robert -- remember.guy do |as, often| as.you_can - without end