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
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 typeruby apachereportis probably ruby 1.9, which does support?<=.To verify that this is the case type in
which rubyand notice that it prints something other than/usr/bin/rubyand/or compare the results of/usr/bin/ruby --versiontoruby --version.