如何将此匹配作为给定的目录

时间:2014-03-04 14:11:12

标签: ruby regex

currDir = ""
# 
# regex is from stack overflow question:
dirRegex = Regexp.new '^(?!.*[\\/]\.{2}[\\/])(?!\.{2}[\\/])[-\w.\\/]+$'
if ARGV.length == 1 && $1.to_s.match dirRegex
  currDir = $1
  puts $1
  puts "#{currDir}"
  puts ARGV.length
else
  currDir = "./"
  puts $1
  puts "#{currDir}"
  puts ARGV.length
end

当我尝试让上面的代码与home或〜/ test /这样的目录匹配时,它会给我一个错误。

./script.rb /home/local/NKU/dixonc3/test
./script.rb:9: syntax error, unexpected tIDENTIFIER, expecting keyword_then or ';' or '\n'
./script.rb:14: syntax error, unexpected keyword_else, expecting $end

1 个答案:

答案 0 :(得分:5)

变化:

if ARGV.length == 1 && $1.to_s.match dirRegex # line 5

为:

if ARGV.length == 1 && $1.to_s.match(dirRegex)