![]() |
Parse a String
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 -- Posted via http://www.ruby-forum.com/. |
Re: Parse a String
On Fri, Jan 22, 2010 at 12:32 PM, Sean Warburton
<sean_warburton@yahoo.co.uk> wrote: > 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. |
Re: Parse a String
|
Re: Extending Rails models
On 23.01.2010 02:02, Jay Crouch wrote:
> Hello, 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. ;) -- Phillip Gawlowski |
| All times are GMT. The time now is 05:46 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.