Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7761857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:14:39+00:00 2026-06-01T14:14:39+00:00

I am reading one file using ruby in which I have written regexp in

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:14:40+00:00Added an answer on June 1, 2026 at 2:14 pm

    Looks good to me.

    A suggestion is to anchor your scan at the start and end of the string by using \A and \z

    /\A...\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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one process who's reading from a file (using file.read() ) and one
I'm having problems reading this one JPEG file using ImageIO.read(File file) - it throws
I am reading an Excel file using xlrd . In one column I have
I am reading one file using Ruby and I am able to filter out
I am reading one property file which contains some file paths using shell script.
I am trying to read an XML file using LINQ. I have had no
I have a CSV file that I want to read with Ruby and create
I am reading in a file using numpy.genfromtxt which brings in columns of both
I wrote the below method to archive files into one file using binary mode:
Hey all. I'm reading from one sql-format file to another, and two bytes in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.