Parse a String

Discussion in 'Ruby' started by Sean Warburton, Jan 22, 2010.

  1. 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/.
     
    Sean Warburton, Jan 22, 2010
    #1
    1. Advertising

  2. On Fri, Jan 22, 2010 at 12:32 PM, Sean Warburton
    <> 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.
     
    Jesús Gabriel y Galán, Jan 22, 2010
    #2
    1. Advertising

  3. Sean Warburton, Jan 22, 2010
    #3
  4. 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
     
    Phillip Gawlowski, Jan 23, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vincent

    parse string

    Vincent, Dec 16, 2003, in forum: Perl
    Replies:
    2
    Views:
    2,818
    Gunnar Hjalmarsson
    Dec 16, 2003
  2. Kevin Joseph
    Replies:
    5
    Views:
    4,350
    Joe Smith
    Aug 13, 2004
  3. Henri
    Replies:
    2
    Views:
    322
    Henri
    Nov 29, 2004
  4. Replies:
    19
    Views:
    1,143
    Daniel Vallstrom
    Mar 15, 2005
  5. 7stud --

    optparse: parse v. parse! ??

    7stud --, Feb 20, 2008, in forum: Ruby
    Replies:
    3
    Views:
    192
    7stud --
    Feb 20, 2008
Loading...

Share This Page