Ruby 使用非转义\字符插入变量的正则表达式
下面我将尝试解析UNRR提取.rar的输出。我有下面一行抛出错误:转义序列太短。这是由于变量extractLocation等于具有未转义字符的文件路径(例如I:\Rar\Backups\)。有没有办法在我的正则表达式中引用这个变量的文本字符串解释?如果没有,我可以采取什么其他方法来解决这个问题Ruby 使用非转义\字符插入变量的正则表达式,ruby,regex,Ruby,Regex,下面我将尝试解析UNRR提取.rar的输出。我有下面一行抛出错误:转义序列太短。这是由于变量extractLocation等于具有未转义字符的文件路径(例如I:\Rar\Backups\)。有没有办法在我的正则表达式中引用这个变量的文本字符串解释?如果没有,我可以采取什么其他方法来解决这个问题 stdoutArr = stdout.split("\n") stdoutArr.each do |line| #line below is throwing the error if line.
stdoutArr = stdout.split("\n")
stdoutArr.each do |line|
#line below is throwing the error
if line.match(/((?<=(Extracting #{extractLocation})).*.bak)/)
extractedFile = line[(/((?<=(Extracting #{extractLocation})).*.bak)/)]
end
end
stdoutar=stdout.split(“\n”)
Stdoutar.每条do |线|
#下面的行抛出了错误
如果line.match(/(?尝试此选项
regexp = /((?<=(Extracting #{Regexp.escape(extractLocation)})).*.bak)/
regexp=/(?试试这个
regexp = /((?<=(Extracting #{Regexp.escape(extractLocation)})).*.bak)/
regexp=/(?