Parse a String

S

Sean Warburton

Hello,

I wonder if someone would be able to help.

Through necessity I'm learning Ruby and trying to write my own script
and while I am getting there and grasping things more, I'm still on
first rung of the ladder and finding things difficult so please be
patient ;)

Part of my script involves opening a telnet session and then sending a
whois request that returns a lot of text ...

I've managed to get to the point where I have the reply stored in a
variable but what I would now like to do is parse the variable for bits
of the information and I have read and read but can't find something
that I seem to be able to understand.

I have attached a text file with the data that my variable holds and I
need to parse out things like the "Renewal date:" and "Registration
status:". Would anyone be able to offer me some help on how I should go
about parsing.

TIA

Warby

Attachments:
http://www.ruby-forum.com/attachment/4407/test.txt
 
J

Jesús Gabriel y Galán

Hello,

I wonder if someone would be able to help.

Through necessity I'm learning Ruby and trying to write my own script
and while I am getting there and grasping things more, I'm still on
first rung of the ladder and finding things difficult so please be
patient ;)

Part of my script involves opening a telnet session and then sending a
whois request that returns a lot of text ...

I've managed to get to the point where I have the reply stored in a
variable but what I would now like to do is parse the variable for bits
of the information and I have read and read but can't find something
that I seem to be able to understand.

I have attached a text file with the data that my variable holds and I
need to parse out things like the "Renewal date:" and "Registration
status:". Would anyone be able to offer me some help on how I should go
about parsing.

You should a bit about regular expressions. If the text has "simple"
rules, regexes are usually enough to extract the pieces of data you
need. In your case, for example:

irb(main):001:0> data =<<EOF
irb(main):002:0" Domain name:
irb(main):003:0" suburbia.co.uk
irb(main):004:0"
[...]

irb(main):053:0> m = data.match(/Renewal date:\s+(.*)/)
=> #<MatchData "Renewal date: 28-Oct-2009" 1:"28-Oct-2009">
irb(main):054:0> m[1]
=> "28-Oct-2009"

If your case is as simple as this, the above solution could be enough.

Hope this helps,

Jesus.
 
P

Phillip Gawlowski


Hello!

First things first: A subject line isn't optional, but helps us in
picking through the mails to find out what we can help with / are
interested in. ;)
I'm new to ruby/rails and am building out models right now.
Thanks in advance for any help/clarification.

Second, you'll have better luck on the Rails mailing lists:
http://rubyonrails.org/community

While every Rails user uses Ruby, not every Ruby user uses Rails. ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top