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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:23:52+00:00 2026-05-31T22:23:52+00:00

I was doing a little test to try Ruby’s pty and I can’t get

  • 0

I was doing a little test to try Ruby’s pty and I can’t get it right. I think the main problem is that the regular expression is non-greedy.

This is a program called inputs.rb:

puts "Give me root@server password:"
password = gets.chomp()
puts "Thank you! Your password is: #{password}"

And this is a program called test.rb:

require 'pty'
require 'expect'

#$expect_verbose = true

#answer = %r/Thank you! Your password is: \w+/

PTY.spawn("ruby ./inputs.rb") do |reader, writer, pid|

   writer.sync = true

   reader.expect(/root@server password:/) do |output|
      writer.puts("password1234")
   end

   #reader.expect(answer) do |output|
   #reader.expect(/Thank you! Your password is: \w{6,}/) do |output|
   #reader.expect(/Thank you! Your password is: (\w+)/) do |output|
   reader.expect(/Thank you! Your password is: \w+/) do |output|
      puts "The whole output is ||||#{output}||||\n"
      output.first.each do |line|
         puts "output1 = |#{line}|\n"
      end
   end

end

Unfortunately, when printing the output I get this:
The whole output is ||||
password1234
Thank you! Your password is: p||||
output1 = |
|
output1 = |password1234
|
output1 = |Thank you! Your password is: p|

Why is it
Thank you! Your password is: p||||
instead of
Thank you! Your password is: password1234||||
?

Is this normal? In case it is: is there any way to change this behaviour?

Things that I have tried:

  • The regular expression on rubular: It works.
  • All the commented alternatives: None of them work (get the complete password).
  • http://www.42klines.com/2010/08/14/what-to-expect-from-the-ruby-expect-library.html

Ruby version: 1.8.7
Ubuntu: 10.04 (Lucid Lynx)

I will appreciate any ideas you may have. Thank you very much.

  • 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-31T22:23:54+00:00Added an answer on May 31, 2026 at 10:23 pm

    Looks like the carriage return is confusing the “+” quantifier.

    The below matches the entire line, including the carriage return (which messes up the output formatting and should be trimmed)

       reader.expect(/Thank you! Your password is: \w+\r/) do |output|
    

    Good luck.

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

Sidebar

Related Questions

Can I get a little help with this? In a test project, I have
im doing a little test to check whether the user is away (unactive) or
OK so that title sucks a little but I could not think of anything
I am doing little assigment which involves fork,vfork and clone function. I need to
Im doing a little messing around and on a learning curve with Magento, Jquery
I was doing a little exploring of a legacy system I maintain, with NDepend
I've been doing a little JavaScript (well, more like jQuery) for a while now
I'm doing a little bit of memory profiling to my software and after running
I'm doing a little experiment, trying to alternate background colours for nested divs. This
I'm planning a web service and doing a little research about what are current

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.