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

  • SEARCH
  • Home
  • 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 8997133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:50:50+00:00 2026-06-15T23:50:50+00:00

I need to scan a 300MB text file with a regex. Reading the whole

  • 0

I need to scan a 300MB text file with a regex.

  • Reading the whole file and taking it into a variable eats over 700MB of RAM and then fails with “cannot allocate memory” error.
  • The match can be in two or three lines, so I cannot use line-to-line stepping in loop.

Is there any lazy method to do a full file scan with a regex without reading it into a separate variable?

UPD

Done. Now you can use this function to read by chunks.
Modify it for your goals.

def prepare_session_hash(fname, regex_string, start=0)
  @session_login_hash = {}
  File.open(fname, 'rb') { |f|
    fsize = f.size
    bsize = fsize / 8
    if start > 0
      f.seek(start)
    end

    overlap = 200

    while true
      if (f.tell() >= overlap) and (f.tell() < fsize)
        f.seek(f.tell() - overlap)
      end
      buffer = f.read(bsize)
      if buffer
        buffer.scan(s) { |match|
          @session_login_hash[match[0]] = match[1]
        }
      else
        return @session_login_hash
      end
    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-15T23:50:51+00:00Added an answer on June 15, 2026 at 11:50 pm
    1. Traverse the file in chunks, instead of line by line, where chunks are created by occurences of a frequently occuring character or pattern , say “X”.
    2. “X” is such that it never exists in your regex, i.e. “X” is where your regex will never match the string.
    3. Match your regex in the current chunk,extract matches and proceed to next chunk.

    Example:

    This is string with multline numbers -2000
    2223434
    34356666
    444564646
    . These numbers can occur at 34345
    567567 places, and on 67
    87878 pages . The problem is to find a good
    way to extract these more than 100
    0 regexes without memory hogging.
    

    In this text, assume the desired pattern is numeric strings e.g /d+/s match digits multiline,
    Then instead of processing and loading whole file, you can chose a chunk creating pattern, say FULL STOP in this case . and only read and process till this pattern, then move to next chunk.

    CHUNK#1:

    This is string with multline numbers -2000
    2223434
    34356666
    444564646
    .
    

    CHUNK#2:

    These numbers can occur at 34345
    567567 places, and on 67
    87878 pages
    

    and so on.

    EDIT:
    Adding @Ranty’s suggestion from the comments as well:

    Or simply read by some amount of lines, say 20. When you find the
    match within, clear up to the match end and append another 20 lines.
    No need for figuring frequently occurring ‘X’.

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

Sidebar

Related Questions

I'm writing a file manager and need to scan directories and deal with renaming
I want to scan FAT32 disk (I just need file path and file name)
I am doing a media file scan using sendBroadcast. Then I need to wait
I need to scan class or jar file that implements or extend a specific
I need to scan an input file and search for a specific value. How
I need to scan a file system for a list of files, and log
I need to scan all files on our solution (aspx, js, ascx, etc) Our
I'm newbie for python, I'm having task so I need to scan wifi and
How do i scan the barcode of format PDF417 ? Do i need to
I am developing an iPhone application, in which i need to write characters scan

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.