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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:34:24+00:00 2026-05-17T02:34:24+00:00

The following code opens a text file does a little regex to match names

  • 0

The following code opens a text file does a little regex to match names and numbers. I do an IF so that I only match numbers greater than 0. When I try to puts the name and number, I only get the numbers and the name have become nil. If I puts the names (variable a) before the if statement, it is there. What am I doing wrong?

nf = File.open("textfile.txt")

nf.each do |b|
 a = b.match(/([\S]+)name([\S]+)/)
 c = b.match(/[0-9]+ numbers/)
 c = c.to_s.split(/ /)
 c = c[0].to_i
  if c > 0
   puts a
   puts c
  end 
 end

textfile looks like this:

My name is Mark
12432 numbers
My name is Joe
0 numbers

I want to be able to puts:
My name is Mark
12432 numbers

and not print out:
My name is Joe
0 numbers

Thanks in advance for your help

  • 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-17T02:34:24+00:00Added an answer on May 17, 2026 at 2:34 am

    Well there are four lines. On lines 1 and 3 there is a name, so a will not be nil. However c > 0 will be false and thus nothing will be printed.

    On lines 2 and 4 there is no name, so a is nil.

    Edit: Also /([\S]+)name([\S]+)/ will never actually match in your example file because “name” is surrounded by spaces (and \S means “not a space”).

    Edit2: Here’s a solution using scan

    File.read("textfile.txt").scan(/My name is (\w+)\n(\d+) numbers/) do |name, num|
      num = num.to_i
      if num > 0
        puts "Name: #{name}"
        puts "Number: #{num}"
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why does the following code sometimes causes an Exception with the contents CLIPBRD_E_CANT_OPEN: Clipboard.SetText(str);
With the following file reading code: using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read,
The following code doesn't compile with gcc, but does with Visual Studio: template <typename
I've been using the following code to open Office Documents, PDF, etc. on my
Following code, when compiled and run with g++, prints '1' twice, whereas I expect
The following code works great in IE, but not in FF or Safari. I
The following code illustrates an object literal being assigned, but with no semicolon afterwards:
The following code should find the appropriate project tag and remove it from the
The following code is in the /Courses/Detail action: [AcceptVerbs(GET)] public ActionResult Detail(int id) {
The following code: template <typename S, typename T> struct foo { void bar(); };

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.