Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby regex works with ruby command but not shebang

I have the following 2 regular expressions in a ruby file. They run fine when i use the ruby command but if i try to run via ./apachereport.rb it generates an error.

regex:

urls = parse(@file, /(?<=GET )\S+/)
codes = parse(@file, /(?<=HTTP\/[0-9]\.[0-9]" )\S+/)

error:

./apachereport.rb:34: undefined (?...) sequence: /(?<=GET )\S+/
./apachereport.rb:47: undefined (?...) sequence: /(?<=HTTP\/[0-9]\.[0-9]" )\S+/

The shebang I'm using is as follows, which seems to work fine with other ruby files:

#!/usr/bin/ruby
like image 596
CoryDorning Avatar asked Apr 28 '26 00:04

CoryDorning


1 Answers

The most likely explanation for this is that you have multiple versions of ruby installed. The version installed in /usr/bin (which is the one you're using in your shebang line) is 1.8.X, which did not support ?<= (look-behind) in regexen. The one you execute when you type ruby apachereport is probably ruby 1.9, which does support ?<=.

To verify that this is the case type in which ruby and notice that it prints something other than /usr/bin/ruby and/or compare the results of /usr/bin/ruby --version to ruby --version.

like image 165
sepp2k Avatar answered Apr 29 '26 18:04

sepp2k