我正在使用Ruby中的HEX中的动态大小的列读取数据库文件。我可以使用以下脚本成功地将文件拆分为记录:
open_file = IO.binread(path + file_name)
record_delimeters = ['FAFA', 'FEFE', 'FDFD']
# regex with bytes is kinda finicky.. So I went this route to avoid the pitfalls of escape characters and gsub... If anyone knows a better way to do this part, I am up for suggestions as well..
final_reg = '['
record_delimeters.each_with_index do |delim, index|
standard_string = '\xFA-\xFA'
standard_string[2,2] = delim[0,2]
standard_string[7,2] = delim[2,2]
unless index == 0
final_reg += '|'
end
final_reg += standard_string
end
final_reg += ']+'
reg = Regexp.new final_reg.encode('UTF-8'), Regexp::IGNORECASE | Regexp::MULTILINE, 'n'
records = open_file.split(reg);nil
但是,我想保留我的分隔符作为参考,因为分隔符表示所有'类型'记录的内容。即:' uint,int,word等...'。
最终我希望记录看起来像这样:
["\xFE\xFE\x00\xF4\x35...", "\xFA\xFA\x03\x4F\x7A...", ...]
或者这个:
["\xFE\xFE", "\x00\xF4\x35...", "\xFA\xFA", "\x03\x4F\x7A...", ...]
但绝对不是这个(这就是我所拥有的):
["\x00\xF4\x35...", "\x03\x4F\x7A...", ...]