I am reading one file using ruby in which I have written regexp in my code as below assuming it will read first 4 bytes from file, next four bytes, next four bytes, rest of the bytes in file except last 8 bytes from file.
Now I am trying to further split bytes from these variable size data (.*) using following code.Is this correct way? I am getting results but still unsure if they are correct or not.
Is this the correct way to scan contents of remaining asterisk part of first regex in first do..end and then again scan remaining bytes in second do..end regex ?
File.open(filename,'rb') do |file|
file.read.scan(/(.{4})(.{4})(.{4})(.*)(.{8})/m).each do |a,b,c,d,e|
puts "\Content 1:#{a}\n\n"
b1 = b.unpack("N")
puts "\n\nContent 2:\n#{b1}\n\n"
puts "\n\nContent 3:\n#{c.unpack("N")}\n\n"
d.scan(/(.{4})(.{4})(.{4})(.*)/).each do |p,q,r,s|
puts "\n\nPContent 4:\n#{p.unpack("N")}\t"
puts "Content 5\n:#{q.unpack("e")}\t"
puts "Content 6:\n#{r.unpack("e")}\t"
s.scan(/(.{4})(.{4})(.{4})(.{4})(.{4})(.{4})(.{4})(.{4})(.{4})(.{4})(.*)/).each do |f,g,h,i,j,k,l,m,n,o,p|
puts "Content 7:#{f.unpack("e")}"
puts "Content 8:#{g.unpack("B")}"
puts "Content 9:#{h.unpack("B")}"
puts "Content 10:#{i.unpack("B")}"
puts "Content 11:#{j.unpack("e")}"
puts "Content 12:#{k.unpack("e")}"
puts "Content 13:#{l.unpack("e")}"
puts "Content 14:#{m.unpack("B")}"
puts "Content 15:#{n.unpack("B")}"
puts "Content 16:\t#{o}#{p}"
end
end
Looks good to me.
A suggestion is to anchor your scan at the start and end of the string by using \A and \z
In my opinion this makes it easier to quickly see that you want to match the entire string, rather than using #scan to iterate on matches. It may perform faster too.