# Parse a String

**URL:** <https://www.ruby-forum.com/t/parse-a-string/182090>\
**Category:** Ruby\
**Created:** [January 22, 2010, 12:32pm UTC](https://www.ruby-forum.com/t/parse-a-string/182090 "2010-01-22T12:32:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![seanwarburton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/2bfe46/32.png) [@seanwarburton](https://www.ruby-forum.com/u/seanwarburton)\
**Post date:** [January 22, 2010, 12:32pm UTC](https://www.ruby-forum.com/t/parse-a-string/182090/1 "2010-01-22T12:32:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![seanwarburton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/2bfe46/32.png) [@seanwarburton](https://www.ruby-forum.com/u/seanwarburton)\
**Post date:** [January 22, 2010, 12:41pm UTC](https://www.ruby-forum.com/t/parse-a-string/182090/2 "2010-01-22T12:41:45Z")

</div>

On Fri, Jan 22, 2010 at 12:32 PM, Sean W.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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](http://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.

---

<div class="post-metadata">

**Author:** ![seanwarburton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/2bfe46/32.png) [@seanwarburton](https://www.ruby-forum.com/u/seanwarburton)\
**Post date:** [January 22, 2010, 12:49pm UTC](https://www.ruby-forum.com/t/parse-a-string/182090/3 "2010-01-22T12:49:50Z")

</div>

Thank you, I’ll try your suggestion.

---

<div class="post-metadata">

**Author:** ![seanwarburton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/2bfe46/32.png) [@seanwarburton](https://www.ruby-forum.com/u/seanwarburton)\
**Post date:** [January 23, 2010, 2:02am UTC](https://www.ruby-forum.com/t/parse-a-string/182090/4 "2010-01-23T02:02:33Z")

</div>

Hello,

I’m new to ruby/rails and am building out models right now.

My question is when extending a has\_many attribute of a model as shown  
below does this cause 2 sequential sql selects, or just one?. Also I am  
wondering what methods are available on the result… i.e. are all the  
methods available to has\_many available (such as length or clear) to the  
result of the sub method.

class Prospect \< ActiveRecord::Base  
has\_many :invitations do  
def of\_campaign(campaign\_id)  
find :all, :conditions =\> [‘campaign\_id = ?’, campaign\_id]  
end  
end  
end

#…some controller…  
AProspect.invitations.of\_campaign(1)…  
#…

> I’m pretty sure the above ruby executes: SELECT \* FROM invitations WHERE prospect\_id = # AND campaign\_id = #  
> But I’m not sure if it also executes SELECT \* FROM invitations WHERE prospect\_id = #  
> Also, I’m not clear as to what methods would be available to the result of ‘of\_campaign’… would all the has\_many methods like length and clear be available.

Thanks in advance for any help/clarification.  
Jay Crouch

---

<div class="post-metadata">

**Author:** ![seanwarburton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/2bfe46/32.png) [@seanwarburton](https://www.ruby-forum.com/u/seanwarburton)\
**Post date:** [January 23, 2010, 3:23am UTC](https://www.ruby-forum.com/t/parse-a-string/182090/5 "2010-01-23T03:23:54Z")

</div>

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:

> **[Community](https://rubyonrails.org/community)**
>
> A web-app framework that includes everything needed to create database-backed web applications according to the Model-View-Controller (MVC) pattern.

While every Rails user uses Ruby, not every Ruby user uses Rails. 😉
