python - 将 python 原始字符串/正则表达式转换为 ruby
问题描述
我目前正在尝试将 python 脚本翻译成 ruby。现在我被困在一个使用原始字符串作为正则表达式的部分。
这是原始的python代码:
pat = re.compile(r'.{4}\xAA\xEE\xAA\x76\x1B\xEC\xBB\x20\xF1\xE6\x51.{1}\x78\x9C')
match = pat.search(string)
(start_match, end_match) = match.span()
这是我尝试将其翻译成 ruby:
pat = Regexp.compile('.{4}\\xAA\\xEE\\xAA\\x76\\x1B\\xEC\\xBB\\x20\\xF1\\xE6\\x51.{1}\\x78\\x9C')
start_match, end_match = string.enum_for(:scan, pat).map { Regexp.last_match.begin(0) }
不幸的是,我一定做错了,因为我收到了这个错误:
invalid multibyte escape: /.{4}\\xAA\\xEE\\xAA\\x76\\x1B\\xEC\\xBB\\x20\\xF1\\xE6\\x51.{1}\\x78\\x9C/ (RegexpError)
我也试过:
regex_String = <<'TEXT'
.{4}\xAA\xEE\xAA\x76\x1B\xEC\xBB\x20\xF1\xE6\x51.{1}\x78\x9C
TEXT
pat = Regexp.compile(regex_String)
start_match, end_match = string.enum_for(:scan, pat).map { Regexp.last_match.begin(0) }
但它会导致同样的错误。
要将其放在上下文中,这是整个脚本:
# Commented lines are the original python code
# Uncommented lines are the translated ruby code
#import zlib
#import sys
#import re
#import binascii
require "zlib"
require "hex_string"
#if(len(sys.argv) < 2 or sys.argv[1] == "-h"):
# print "usage: python DecompNewDell.py <biosupdate.exe>"
# exit()
if ARGV.length < 1 or ARGV[0] == "-h"
puts "usage: ruby DecompNewDell.rb <biosupdate.exe>";
exit
end
#f = open(sys.argv[1], "rb")
#string = f.read()
f = File.open(ARGV[0], 'rb')
string = f.read
#pat = re.compile(r'.{4}\xAA\xEE\xAA\x76\x1B\xEC\xBB\x20\xF1\xE6\x51.{1}\x78\x9C')
#match = pat.search(string)
#(start_match, end_match) = match.span()
pat = Regexp.compile('.{4}\\xAA\\xEE\\xAA\\x76\\x1B\\xEC\\xBB\\x20\\xF1\\xE6\\x51.{1}\\x78\\x9C')
start_match, end_match = string.enum_for(:scan, pat).map { Regexp.last_match.begin(0) }
#compessed_len = string[start_match:start_match+4]
compressed_len = string[start_match..start_match+4]
#compessed_len = binascii.b2a_hex(compessed_len[::-1])
compessed_len.reverse!
compessed_len = compessed_len.to_hex_string(false)
#compessed_len = long(compessed_len, 16)
compessed_len = compessed_len.to_i(16)
#read len bytes out of the file into the new string to decompress
#f.seek(start_match+16)
#string = f.read(compessed_len)
f.seek start_match+16
string = f.read compessed_len
#o = zlib.decompress(string)
o = Zlib::Inflate.inflate(string)
#f2 = open(sys.argv[1] + "_decompressed.hdr", "wb")
#f2.write(o)
#f.close()
#f2.close()
#print "Decompressed data written to %s_decompressed.hdr" % sys.argv[1]
f2 = File.open(ARGV[0] + "_decompressed.hdr", 'wb')
f2.write(o)
f.close()
f2.close()
puts "Decompressed data written to #{ARGV[0]}_decompressed.hdr"
解决方案
这个答案说明了为什么会出现问题。
https://stackoverflow.com/a/47785810/12349985
并且有针对这种情况的解决方案。
https://techoverflow.net/2013/12/29/solving-invalid-multibyte-escape-xfexff-in-ruby-vpim/
推荐阅读
- javascript - 每个元素的输入数字对象
- azure - 如何使用 Azure Rest API 在 azure 门户中的诊断设置列表下获取资源的诊断状态?状态如下:启用/禁用
- python - 布尔索引与维度 0 上的索引数组不匹配;维度为 2,但对应的布尔维度为 18
- rabbitmq - 如何查询rabbitmq_exporter
- websocket - 如何在 WebSocket 中代理到多个服务器
- flutter - 允许小部件溢出
- java - SmsManager 在发送每条短信之间有延迟
- image - 上传后在编辑器中插入图片
- go - 执行 golang syscall.MProtect 时导致权限被拒绝
- protractor - 如何在量角器自定义定位器中执行浏览器方法?