# Parsing text in a rails application

**URL:** <https://www.ruby-forum.com/t/parsing-text-in-a-rails-application/170206>\
**Category:** Rails\
**Created:** [June 14, 2009, 12:19am UTC](https://www.ruby-forum.com/t/parsing-text-in-a-rails-application/170206 "2009-06-14T00:19:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hypnotizing\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/c37758/32.png) [@Hypnotizing\_C](https://www.ruby-forum.com/u/Hypnotizing_C)\
**Post date:** [June 14, 2009, 12:19am UTC](https://www.ruby-forum.com/t/parsing-text-in-a-rails-application/170206/1 "2009-06-14T00:19:06Z")

</div>

I have a piece of text which is in the following format:  
“1. Question blah blah blah. goes on blah blah  
blah blah.  
Answer: Answer blah, blah goes on like  
this blah blah.  
2. One more question. blah blah goes  
on like this again.  
Answer: Again and again, over and over.”

Now taking a piece of text, I have to break it down to pairs of  
question and answer sets so that they can be saved in to question  
model - which has a question and answer attribute. A single piece of  
text saved in a Post model needs to be parsed into a number of  
question models.  
The problem is that the text is very idiosyncratic, with numerous line  
breaks and carriage returns inside each question and answer but the  
numbering and the “Answer:” segments are present accurately. From what  
I read through various sources, this can be accomplished through  
regular expressions but I am not able to figure out how to go about  
this. Could someone explain how this can be accomplished for this  
particular case?

---

<div class="post-metadata">

**Author:** ![Hypnotizing\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/c37758/32.png) [@Hypnotizing\_C](https://www.ruby-forum.com/u/Hypnotizing_C)\
**Post date:** [June 14, 2009, 12:20am UTC](https://www.ruby-forum.com/t/parsing-text-in-a-rails-application/170206/2 "2009-06-14T00:20:51Z")

</div>

On Jun 13, 9:40 pm, Hypnotizing C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> question and answer sets so that they can be saved in to question  
> model - which has a question and answer attribute. A single piece of  
> text saved in a Post model needs to be parsed into a number of  
> question models.  
> The problem is that the text is very idiosyncratic, with numerous line  
> breaks and carriage returns inside each question and answer but the  
> numbering and the “Answer:” segments are present accurately. From what  
> I read through various sources, this can be accomplished through  
> regular expressions but I am not able to figure out how to go about  
> this. Could someone explain how this can be accomplished for this  
> particular case?

you may find string scanner more appropriate in this case. In a  
nutshell you tell it to scan until it finds text matching a particular  
pattern (in your case the beginning of question and beginning of  
answer markers), and it tells you all the text it consumed in between.

Fred

---

<div class="post-metadata">

**Author:** ![Hypnotizing\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/c37758/32.png) [@Hypnotizing\_C](https://www.ruby-forum.com/u/Hypnotizing_C)\
**Post date:** [June 14, 2009, 8:54am UTC](https://www.ruby-forum.com/t/parsing-text-in-a-rails-application/170206/3 "2009-06-14T08:54:12Z")

</div>

On 14 Jun, 03:20, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 1. One more question. blah blah goes  
> > numbering and the “Answer:” segments are present accurately. From what  
> > Fred  
> > Thanks! String scanner worked like a charm.

---

<div class="post-metadata">

**Author:** ![Hypnotizing\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/c37758/32.png) [@Hypnotizing\_C](https://www.ruby-forum.com/u/Hypnotizing_C)\
**Post date:** [June 15, 2009, 11:39am UTC](https://www.ruby-forum.com/t/parsing-text-in-a-rails-application/170206/4 "2009-06-15T11:39:49Z")

</div>

Hi,  
You can use Faster csv plugin for this.

On Jun 15, 5:09 am, Anandh K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Hypnotizing\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/c37758/32.png) [@Hypnotizing\_C](https://www.ruby-forum.com/u/Hypnotizing_C)\
**Post date:** [June 15, 2009, 11:39am UTC](https://www.ruby-forum.com/t/parsing-text-in-a-rails-application/170206/5 "2009-06-15T11:39:49Z")

</div>

Hi,  
youc an use Faster Csv plugin for this. It’s very easy by this  
plugin.

2009/6/15 Anandh K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> ## Thanks in Advance
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Regards,

Amit K.  
Sr. Software Engrr  
+91 9890654102  
+91 9373087399  
Blogsite: [http://amitbaraik.blog.co.in/](http://amitbaraik.blog.co.in/)

---

<div class="post-metadata">

**Author:** ![Hypnotizing\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/c37758/32.png) [@Hypnotizing\_C](https://www.ruby-forum.com/u/Hypnotizing_C)\
**Post date:** [June 15, 2009, 11:49am UTC](https://www.ruby-forum.com/t/parsing-text-in-a-rails-application/170206/6 "2009-06-15T11:49:15Z")

</div>

hi amit

Thanks for your reply… can i use string scanner class to accomplish  
the task…

---

<div class="post-metadata">

**Author:** ![Hypnotizing\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/c37758/32.png) [@Hypnotizing\_C](https://www.ruby-forum.com/u/Hypnotizing_C)\
**Post date:** [June 15, 2009, 11:09am UTC](https://www.ruby-forum.com/t/parsing-text-in-a-rails-application/170206/7 "2009-06-15T11:09:00Z")

</div>

Hi there

```
 I m a starter in both linux and ruby on rails... is there any ruby 

```

gems or any other way to parse the text from open office and upload it  
to the database…

Thanks in Advance

---

<div class="post-metadata">

**Author:** ![Hypnotizing\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/c37758/32.png) [@Hypnotizing\_C](https://www.ruby-forum.com/u/Hypnotizing_C)\
**Post date:** [June 15, 2009, 12:36pm UTC](https://www.ruby-forum.com/t/parsing-text-in-a-rails-application/170206/8 "2009-06-15T12:36:42Z")

</div>

Hi everybody

```
   Can i use stringscanner class to parse strings of an openoffice 

```

document and upload it to the datbase… If so can anyone there guide me  
in fixing this task… I want this task to be done in ruby on rails and  
in linux platform…

Thanks in advance
