Ruby 正则表达式适用于 ruby 命令,但不适用于 shebang
我的 ruby 文件中有以下 2 个正则表达式。当我使用 ruby 命令时它们运行良好,但如果我尝试通过 ./apachereport.rb 运行它会生成错误。
正则表达式:
urls = parse(@file, /(?<=GET )\S+/)
codes = parse(@file, /(?<=HTTP\/[0-9]\.[0-9]" )\S+/)
错误:
./apachereport.rb:34: undefined (?...) sequence: /(?<=GET )\S+/
./apachereport.rb:47: undefined (?...) sequence: /(?<=HTTP\/[0-9]\.[0-9]" )\S+/
我正在使用的 shebang 如下,它似乎与其他 ruby 文件一起工作正常:
#!/usr/bin/ruby
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最可能的解释是您安装了多个版本的 ruby。安装在
/usr/bin
中的版本(即您在 shebang 行中使用的版本)是 1.8.X,它不支持?<=
(正则表达式中的后视)。当您输入ruby apachereport
时执行的可能是 ruby 1.9,它支持?<=
。要验证情况是否如此,请在
which ruby
中键入,并注意它打印的内容不是/usr/bin/ruby
和/或比较/ 的结果usr/bin/ruby --version 至
ruby --version
。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 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
toruby --version
.