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 3962634
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:07:11+00:00 2026-05-20T03:07:11+00:00

Please forgive my ignorance, I am new to Ruby. I know how to search

  • 0

Please forgive my ignorance, I am new to Ruby.

I know how to search a string, or even a single file with a regular expression:

str = File.read('example.txt')
match = str.scan(/[0-9A-Za-z]{8,8}/)

puts match[1]

I know how to search for a static phrase in multiple files and directories

pattern = "hello"

Dir.glob('/home/bob/**/*').each do |file|
    next unless File.file?(file)
        File.open(file) do |f|
            f.each_line do |line|
                puts "#{pattern}" if line.include?(pattern)
        end
    end
end

I can not figure out how to use my regexp against multiple files and directories. Any and all help is much appreciated.

  • 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-05-20T03:07:11+00:00Added an answer on May 20, 2026 at 3:07 am

    Well, you’re quite close. First make pattern a Regexp object:

    pattern = /hello/
    

    Or if you are trying to make a Regexp from a String (like passed in on the command line), you might try:

    pattern = Regexp.new("hello")
    # or use first argument for regexp
    pattern = Regexp.new(ARGV[0])
    

    Now when you are searching, line is a String. You can use match or scan to get the results of it matching against your pattern.

    f.each_line do |line|
      if line.match(pattern)
        puts $0
      end
      # or
      if !(match_data = line.match(pattern)).nil?
        puts match_data[0]
      end
      # or to see multiple matches
      unless (matches = line.scan(pattern)).empty?
        p matches
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a brand new programming student, so please forgive my ignorance. My assignment
All, Please forgive my ignorance of C#/.NET, I am absolutely new to both (mostly
Please forgive my ignorance I am new to Python development. Is there a tool
I am relatively new to columnar database, please forgive ignorance. Lets say I have
first, please forgive my ignorance and inability so use the search engine (i swear
Please forgive the ignorance, but I'm very new to PHP and I couldn't find
Please forgive my ignorance, but I am very new to IOC and NinJect. I
Ok Guys please forgive my ignorance. I'm new to javascript so all help is
Please forgive my ignorance, but I wasn't able to find an answer elsewhere. I
I don't have much experience with GPUs so please forgive my ignorance. Nowadays, GPUs

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.