首页 > 解决方案 > 将 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"

标签: pythonregexrubystring

解决方案


这个答案说明了为什么会出现问题。
https://stackoverflow.com/a/47785810/12349985

并且有针对这种情况的解决方案。
https://techoverflow.net/2013/12/29/solving-invalid-multibyte-escape-xfexff-in-ruby-vpim/


推荐阅读